Question : Batch file for copying folder named as yesterday

How to display the date of yesterday?

I wanna create a batch file to copy all files in D:\YYYYMMDD to D:\BACKUP\YYYYMMDD
where YYYYMMDD is the date of yesterday.

I have written a code below, but I do not know how to get the date of yesterday, anyone can help?
Code Snippet:
1:
2:
3:
@echo off
 
for /f "tokens=1-3 delims=/ " %%a in ('date/t') do set var=BACKUP %%c-%%a-%%b

Answer : Batch file for copying folder named as yesterday

Three ways I would use for this.

1) Schedule the script to run at 23:59 at night... so "Today" is "yesterday" effectively.

2) Each day have a scheduled script run which dumps the current date into a file.... have this run late at night AFTER your script runs so the date in that file is the previous days... then read it with a for command or set /p

3) Or the most foolproof as it doesn;t rely on another file or schedule, calculate Yesterday... this is quite hard but just about do-able with a long batch file, or you can use a hybrid VBScript and batch file as VBScript understands dates.  I have a little article below on using dates from VBScript like this:

http://www.experts-exchange.com/articles/OS/Microsoft_Operating_Systems/MS_DOS/Using-dates-in-batch-files-scripts.html

To get yesterday in VBS all you need to do is:

@echo off
echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a
echo %yesterday%

which isn't as bad as it looks....

this line
echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2)  > "%temp%\dateparts.vbs"


creates a temporayy VBScript file which outputs the year, month, day as yyyymmdd then the for line reads the output of tha VBScript and directs it to the variable %yesterday% which you can then use in your batch...

Steve
Random Solutions  
 
programming4us programming4us