0

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

Rocky
  • 45
  • 1
  • 2
  • 8
  • 1
    [Batch file to add today's date, yesterday's date, previous day's date as extension to file name](http://stackoverflow.com/questions/9143834/batch-file-to-add-todays-date-yesterdays-date-previous-days-date-as-extensi) or [Append +1 to date in batch file](http://stackoverflow.com/questions/18133210/append-1-to-date-in-batch-file) for example... – gmo Dec 27 '13 at 07:39
  • Do you run batch everyday? IE, did batch run yesterday so we can store yesterday date? – LS_ᴅᴇᴠ Dec 27 '13 at 09:20
  • @LS_dev I do run the batch every night – Rocky Dec 27 '13 at 09:24

4 Answers4

0

You will have to use a variable and do the math:

set /a day=%date:~7,2% - 1
ren SampleDTE.TXT SampleDTE-%date:~10,4%%day%%date:~4,2%_%time:~0,2%%time:~3,2%.TXT
charlesw
  • 572
  • 6
  • 25
  • Ooh - be careful! There's a couple of gotchas here. First is that if the day is <10, the leading '0' will be suppressed - and the `0`th of a month seems a little odd. Second is that a leading `0` as part of a `SET/A` statement means that the element is in OCTAL, so on day=08 or day=09, the `set` will fail because `08` and `09` are not valid octal numbers. A cure is `set /a day=1%date:~7,2% - 1` and then use `%day:~-2%` in the `ren` string. Trying to get the last day of last month to play nice is not straight-forward... – Magoo Dec 27 '13 at 09:11
  • Ahhhh... @Magoo Thanks I will make these changes. Still waiting to try out the other one you answered earlier. – Rocky Dec 27 '13 at 09:21
0

The easy way - assuming that you run this regularly, once per day

FOR /f %%a IN (sampledteyesterday.txt) DO ECHO ren SampleDTE.TXT SampleDTE-%%a_%time:~0,2%%time:~3,2%.txt
> sampledteyesterday.txt ECHO %date:~10,4%%day%%date:~4,2%

Note - ren command simply ECHOed. when verified, remove the ECHO keyword before the REN to activate.

You'll need to set up your sampledteyesterday.txt file containing a single line YYYYDDMM for yesterday to initialise.

Suggestion: use YYYYMMDD which sorts easier or more logically...

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • yes I run this regularly once a day. I tried using your command but it always returns '%%a was unexpected at this time.' . I have created a **sampledteyesterday.txt** containing YYYYMMDD . What's that I am missing? – Rocky Dec 27 '13 at 09:03
  • It always returns...what? – Magoo Dec 27 '13 at 09:05
  • You seem to be running the command from the prompt. When running a command from the prompt, you need to reduce all usages of the metavariable `%%a` (in this case) to `%a`. It's so much easier to run this as a batch file though - double-plus immune to tyops. Take the lines, as they are, pop a line `@echo off` as the first line and `setlocal` as a second and save as a file named (eg) `rensampledte.bat`. From the prompt, `rensampledte` will then execute the batch file. With a little work, you can then schedule it to run using task scheduler, so it would run automatically once per day. – Magoo Dec 27 '13 at 09:29
  • Yup i was trying it in cmd line to test. Now I tried in cmd & .bat this works real good. Thanks a lot. This batch kicks depending on the success of other. So no need to schedule time. – Rocky Dec 27 '13 at 09:45
0

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.

@echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "date-yesterday=%yyyy%-%mm%-%dd%"

echo Yesterday was "%date-yesterday%"
pause
foxidrive
  • 40,353
  • 10
  • 53
  • 68
0

To avoid date arithmetics, you can store yesterday date in, eg, file.

yesterday.txt (contains today and yesterday):

20131227 20131226

Batch file:

REM Get today (to check if yesterday.txt is valid):
SET today=%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%
REM Read file:
FOR /F "TOKENS=1,2" %%d IN (yesterday.txt) DO (
    SET stored_today=%%d
    SET yesterday=%%e
)
REM If stored_today not equal to today, assume yesterday is stored_today and update file:
IF NOT "%stored_today%" == "%today%" (
    SET yesterday=%stored_today%
    >yesterday.txt ECHO %stored_today% %today%
)
REM Test if yesterday is set, exit otherwise.
IF "%yesterday%"=="" ECHO Yesterday unknown! Try again tomorrow.&GOTO:EOF

To make it work correctly first time, yesterday.txt must be manually filled.

LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46