Link to home
Start Free TrialLog in
Avatar of rilliam
rilliamFlag for United States of America

asked on

How to Rename a file where current date get added to its name in DOS Command Prompt?

I want to make a small backup solution by using dos.
I want to create a batch file to make an automatic daily copy from a file saved on drive c:\ to drive d:\ . I don't want the file be over written and need to keep the old files.
I want to rename the file after copying where current date gets added to its name.

Here whats I tried to do but none of them worked :

A: set filename=ABCD.txt
set newfilename=%DATE%_%TIME%_%filename%
copy C:\FOLDER\%filename%  D:\FOLDER\%newfilename%    
I tried with and without " "

B: copy C:\FOLDER\a.txt  D:\FOLDER\a.txt
rename d:\Folder\a.txt "%dte%"

I cannot figure out what the problem is and what I should do. The OS of machine is Windows 2003 server. Should I upgrade Command Promt?

ASKER CERTIFIED SOLUTION
Avatar of ltlbearand3
ltlbearand3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why not just use ntbackup and let it take care of everything for you...

eb
Avatar of rilliam

ASKER

Thanks. I could use your solution on Rename Command but still have problem with copying files. I had same problem "the system cannot find the file specified". I even used "" but it didn't help. I am so excited about the Rename command. It does what I want, thanks
Avatar of rilliam

ASKER

Thanks EB I am kind of beginner in this matter and don't know all the command yet. I surely will work with ntbackup command. thanks.
You can always use the echo command to help troubleshoot batch commands.  I don't know which option is causing trouble or both.  You try the following to make sure it is all lining up and also try to enter the commands manually from a command prompt. Make sure that the file path and name displayed in the Dos window matches where the file actually exits.

When I do a copy and paste from your code I get a special character - that may be your issue
Echo Off
set filename=a.txt
set newfilename=%Date:/=%%Time::=.%.%filename%
Echo Copy File From
Echo C:\Folder\%filename%
Echo Copy To
Echo D:\Folder\%newfilename%
Copy "C:\Folder\%filename%" "D:\Folder\%newfilename%"
Pause
 
Or
 
copy C:\FOLDER\a.txt  D:\FOLDER\a.txt
Echo File
Echo C:\FOLDER\a.txt
Echo Will be Renamed to 
Echo a_%Date:/= % %Time::=.%.txt
REN "C:\FOLDER\a.txt" "a_%Date:/= % %Time::=.%.txt"
Pause

Open in new window