DATA Step, Macro, Functions and more

Error while using rename function.

Reply
Frequent Contributor
Posts: 122

Error while using rename function.

[ Edited ]

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;

Trusted Advisor
Posts: 1,018

Re: Error while using rename function.

[ Edited ]

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');

Super User
Super User
Posts: 7,039

Re: Error while using rename function.

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.

Trusted Advisor
Posts: 1,018

Re: Error while using rename function.

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.

Frequent Contributor
Posts: 122

Re: Error while using rename function.

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

Super User
Posts: 19,772

Re: Error while using rename function.

Post your code and log.

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

PROC Star
Posts: 7,468

Re: Error while using rename function.

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

 

Trusted Advisor
Posts: 1,018

Re: Error while using rename function.

 

 


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

Frequent Contributor
Posts: 122

Re: Error while using rename function.

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,

Trusted Advisor
Posts: 1,018

Re: Error while using rename function.

And the log showing the error message?
Frequent Contributor
Posts: 122

Re: Error while using rename function.

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

 

 

Trusted Advisor
Posts: 1,018

Re: Error while using rename function.

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

PROC Star
Posts: 7,468

Re: Error while using rename function.

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

 

Frequent Contributor
Posts: 122

Re: Error while using rename function.

[ Edited ]

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!

Ask a Question
Discussion stats
  • 13 replies
  • 142 views
  • 0 likes
  • 5 in conversation