samiam41
asked on
Rename file if exist to file.old.%date%
Hey Experts. I am running a script that is dumping out data from our AD environment and the data goes into a log file. I run this script a couple times a week and I have to remember to rename the file .date before running the script. Because my memory is not the best, I would like the batch script to run and if the log file already exists that the existing file will be renamed to .old.%date%. Example:
data.log should be renamed to data.old.060413
The format of the date doesn't matter. It could be julian date or month/date. I just need it to be renamed. So far all I have is:
I appreciate your help!
data.log should be renamed to data.old.060413
The format of the date doesn't matter. It could be julian date or month/date. I just need it to be renamed. So far all I have is:
if exist c:\tools\file.log rename c:\tools\file.old.%date%
I appreciate your help!
There are a couple of ways of incorporating the date into a file name - if you are based in the USA with a US style date format, you can reference the date in a batch file using the following format:
%date:~-4%-%date:4,2%-%dat e:7,2%
That should output a date in the format:
2013-06-04
YYYY-MM-DD (best for sorting)
Other methods exist for retrieving the date values and assigning them to a variable for use in a batch file, but this is the quickest if your date variables match mine.
%date:~-4%-%date:4,2%-%dat
That should output a date in the format:
2013-06-04
YYYY-MM-DD (best for sorting)
Other methods exist for retrieving the date values and assigning them to a variable for use in a batch file, but this is the quickest if your date variables match mine.
To be clear:
if exist c:\tools\file.log rename c:\tools\file.old.%date:~- 4%-%date:4 ,2%-%date: 7,2%
if exist c:\tools\file.log rename c:\tools\file.old.%date:~-
ASKER
Hey leew!! Great to see you again.
That works for me. So, how do I do it? :)
rename file.log file.date.log
That works for me. So, how do I do it? :)
rename file.log file.date.log
ASKER
Oops. Looks like you already posted it. Thanks Leew. Testing it now.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
leew, when I run your command from a batch script, this is what I get:
C:\Tools>if exist c:\tools\file.log rename c:\tools\file.old.2013-dat e:4,2date: 7,2
Nothing shows up in the directory.
C:\Tools>if exist c:\tools\file.log rename c:\tools\file.old.2013-dat
Nothing shows up in the directory.
ASKER
oBdA, just entered in your code, removed the safety cap (echo on line 9) and rolled it out. Worked like I expected! Thanks.
For reference Leew just missed two ~ out of the date commands. You can see what the bits used mean in set /? etc.
if exist c:\tools\file.log rename c:\tools\file.old.%date:~- 4%-%date:~4,2%-%date:~7,2%
if exist c:\tools\file.log rename c:\tools\file.old.%date:~-
ASKER
Thanks dragon-it but I'm not sure what I'm doing wrong. Now I'm getting
"The syntax of the command is incorrect."
I got a solution. I'll leave it as an Unsolved Mystery.
"The syntax of the command is incorrect."
I got a solution. I'll leave it as an Unsolved Mystery.
ASKER
Thanks experts! Rolling with my solution and on to the next challenge. Take care guys.
For example:
file.date.log