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;
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');
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.
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.
For some reason my code is not working. I tried renaming a directory and it worked but not the file renaming.
Post your code and log.
What version of SAS are you using, are you certain the function is available in your version?
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 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
Absolutely!
Will modify my note.
Thx
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,
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
Thank you for posting the log.
Some conjectures:
xx
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.