I am trying to rename some log files to yesterday's date when the batch file creates a new file of same name every night.
We can rename the file to today's date using the below cmd
ren SampleDTE.TXT SampleDTE-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%.TXT
This results in file renamed to // SampleDTE-YYYYDDMM_hhmm.TXT
SampleDTE-20132712_1243.TXT
I wanted to know how to re-name the file to yesterday's date. Something like
SampleDTE-20132612_1243.TXT
Thanks in advance
The easy way - assuming that you run this regularly, once per day
Note -
ren
command simplyECHO
ed. when verified, remove theECHO
keyword before theREN
to activate.You'll need to set up your
sampledteyesterday.txt
file containing a single lineYYYYDDMM
for yesterday to initialise.Suggestion: use
YYYYMMDD
which sorts easier or more logically...To avoid date arithmetics, you can store yesterday date in, eg, file.
yesterday.txt
(contains today and yesterday):Batch file:
To make it work correctly first time,
yesterday.txt
must be manually filled.You will have to use a variable and do the math:
This will get yesterdays date, using VBS in a batch file.
It's reliable in all locales, whereas the %date% variable can be different on different computers, and different users.