BookmarkSubscribeRSS Feed
renjithr
Quartz | Level 8

Hi,

 

I am trying to rename a .txt file on my desktop. I ran the below code but I got the error that 'Invalid file name'.

 

Could you please let me know what I am doing wrong:

 

data tt;

rc=rename('C:\Users\user1\Desktop\Test\testf.txt', 'C:\Users\archive\test2.txt', 'file');

put 'RC:' rc;
if rc then _msg_=sysmsg();

run;

13 REPLIES 13
mkeintz
PROC Star

Are you forgetting a username in the first argument of the rename function?

 

I.e.

rc=rename('C:\Users\myusername\Desktop\Test\testf.txt', 'C:\Users\archive\test2.txt', 'file');

 

rc=rename('C:\Users\myusername\Desktop\Test\testf.txt', 'C:\Users\myusername\archive\test2.txt', 'file');

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

If SAS is just calling the native Windows RENAME command then it does not allow changing the path of the file. Just the name of the file in the same directory.

mkeintz
PROC Star

I used this code to successfully "rename" a file from c:\temp\t.txt  to c:\temp\tmp\t.txt, so I guess it's not actually calling the windows rename command.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
renjithr
Quartz | Level 8

For some reason my code is not working. I tried renaming a directory and it worked but not the file renaming.

Reeza
Super User

Post your code and log.

What version of SAS are you using, are you certain the function is available in your version?

art297
Opal | Level 21

I noticed that you added the username (i.e., user1) in the from name, but you didn't do that for the to name:

 

rc=rename('C:\Users\user1\Desktop\Test\testf.txt', 'C:\Users\archive\test2.txt', 'file');

 

Should the to name have been: C:\Users\user1\archive\test2.txt' abd does that directory exist (i.e., C:\Users\user1\archive )?

 

Art, CEO, AnalystFinder.com

 

mkeintz
PROC Star

 

 


@art297 wrote:

I noticed that you added the username (i.e., user1) in the from name, but you didn't do that for the to name:

 

rc=rename('C:\Users\user1\Desktop\Test\testf.txt', 'C:\Users\archive\test2.txt', 'file');

 

Should the to name have been: C:\Users\user1\archive\test2.txt' abd does that directory exist (i.e., C:\Users\user1\archive )?

 

Art, CEO, AnalystFinder.com

 


@art297

 

Absolutely! 

Will modify my note.

 

Thx

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
renjithr
Quartz | Level 8

Here is my code:

 

data tt;
rc=rename('C:\Users\user1\Desktop\Test\testf.txt', 'C:\Users\user1\Desktop\Test\test2.txt', 'file');
put 'RC:' rc;
if rc then _msg_=sysmsg();


run;

 

I am using SAS 9.4

 

Thanks,

mkeintz
PROC Star
And the log showing the error message?
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
renjithr
Quartz | Level 8

Here you go:

 

682 data tt;
1683 rc=rename('C:\Users\user1\Desktop\Test\testf.txt', 'C:\Users\user1\Desktop\Test\test2.txt',
1683! 'file');
1684 put 'RC:' rc;
1685 if rc then _msg_=sysmsg();
1686 put _msg_;
1687
1688 run;

RC:1
ERROR: Invalid physical name.
NOTE: The data set WORK.TT has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.00 seconds

 

 

mkeintz
PROC Star

Thank you for posting the log.

 

Some conjectures:

 

  1. Are you running the program under the userid whose desktop contents is being renamed.  Would that create a permission issue masquerading as a physical file not exist message?

  2. Is your desktop screen showing a shortcut instead of a file?

  3. Is the file really there?  Perhaps you could pipe input from a dir command to see if the dataset name is exactly as the rename command expects?  I.e.

        filename dslist pipe 'dir/b c:\Users\user1\Desktop\Test';
        data filenames;
          infile dslist  truncover;
          input fname :$60.;
          if fname='testf.txt' then found=1;
          put fname=;
        run;

    If found never=1 then I guess the file really isn't there.      

 

xx

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
art297
Opal | Level 21

I agree with @mkeintz. The function even works on SAS UniversityEdition. e.g. the following worked perfectly:

 

data tt;
  rc=rename('/folders/myfolders/users/user1/desktop/test/testf.txt', '/folders/myfolders/users/user1/desktop/test/test2.txt', 'file');
  put 'RC:' rc;
  if rc then _msg_=sysmsg();
run;
;

Art, CEO, AnalystFinder.com

 

renjithr
Quartz | Level 8

Thank you @mkeintz and @art297 for your suggestions. Yes, the file name was wrong..the extension was .txt' instead of .txt

 

I changed it and now it is working fine.

 

Thanks again!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 1587 views
  • 0 likes
  • 5 in conversation