user2908171
user2908171

Reputation:

Use batch script to copy files with date

I have the following batch file which runs every morning. It copies a file which has the day on it minus one day. So it copies the file today but with yesterday date. The folder is the year and month. The problem with this script is it fails every first day of each month. This is because it is looking for the current month and not the previous month. How can i change it so it will work for the current month but then on the first day of the new month it will look for the previous month?

    rem @echo off

set year=%date:~-4,4%
set month=%date:~-7,2%
set YearMonth=%year%%month%
echo %YearMonth%


echo d = DateAdd^("d", -1, Date^(^)^)>yesterday.vbs
echo wscript.echo DatePart^("yyyy", d^) ^& "/" ^& DatePart^("m", d^) ^& "/"       ^& DatePart^("d", d^)>>yesterday.vbs
for /f "tokens=1-3 delims=/" %%I in ('cscript /nologo yesterday.vbs') do (
set Year=%%I
if %%J LEQ 9 (set Month=0%%J) else set Month=%%J
if %%K LEQ 9 (set Day=0%%K) else set Day=%%K
)
set Day=%Day%
del /q yesterday.vbs
echo %Day%


copy D:\OPMS\Zeag\CC\%YearMonth%\N\creasc.%Day% D:\OPMS\Zeag\CC\BizzExtract

thank you very much for your help

Upvotes: 3

Views: 603

Answers (1)

Stephan
Stephan

Reputation: 56180

you are calculating the complete datestring for yesterday, but then you use only the day. %yearmonth% is still the old value.

Just add

set yearmonth=%Year%%Month%

before your copy-line to update it with yesterdays values.

Upvotes: 1

Related Questions