irshan syed
irshan syed

Reputation: 89

Get Previous and day before Previous date in batch file(cmd)

I have the following to determine the date and grab a file based on date. I need to modify this grab a file that has a date of yesterday and day before yesterday, i.e. date-1 and date-2. What do I need to change? Thanks!

echo @echo off > uploadsp.txt
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
echo set mydate=%date:~10,4%%date:~4,2%%date:~7,2% >> uploadsp.txt

set myfile=Epic_DSH360144_Drug_Utilization_%mydate%_DU.txt
echo put %myfile% >> uploadsp.txt
exit

Upvotes: 1

Views: 3847

Answers (3)

Squashman
Squashman

Reputation: 14340

You can also call out to powershell very easily to grab the date and subtract a day from it.

For /F "delims=" %%G In ('PowerShell -Command "&{((Get-Date).AddDays(-1)).ToString('MMddyyyy')}"') Do Set "yesterday=%%G"

Upvotes: 3

Abhijeetk431
Abhijeetk431

Reputation: 846

This should do the trick.

@echo off
set nd=-1
set nd=-2
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
echo mydate=%date:~10,4%%date:~4,2%%date:~7,2%

echo s=DateAdd("d",%nd%,now) : d=weekday(s) >"%temp%\%~n1.vbs"
echo WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2) >>"%temp%\%~n1.vbs"
for /f %%a in ('cscript /nologo "%temp%\%~n1.vbs"') do set "result=%%a"
del "%temp%\%~n1.vbs"
set yday=%result%

echo s=DateAdd("d",%nd%,now) : d=weekday(s) >"%temp%\%~n2.vbs"
echo WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2) >>"%temp%\%~n2.vbs"
for /f %%a in ('cscript /nologo "%temp%\%~n2.vbs"') do set "result=%%a"
del "%temp%\%~n2.vbs"
set yyday=%result%

set myfile=Epic_DSH360144_Drug_Utilization_%mydate%_DU.txt
set yfile=Epic_DSH360144_Drug_Utilization_%ydate%_DU.txt
set yyfile=Epic_DSH360144_Drug_Utilization_%yydate%_DU.txt
echo put %myfile% >> uploadsp.txt
echo put %yfile% >> uploadsp.txt
echo put %yyfile% >> uploadsp.txt
pause

Upvotes: 0

user7818749
user7818749

Reputation:

Something like this should do.

@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 "result1=%yyyy%-%mm%-%dd%"

set day=-2
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 "result2=%yyyy%-%mm%-%dd%"
set yesterdayFile=Epic_DSH360144_Drug_Utilization_%result1%_DU.txt
set previousdayFile=Epic_DSH360144_Drug_Utilization_%result2%_DU.txt
echo Yesterday: "%result1%" - File to delete is %yesterdayFile%
echo Previous: "%result2%" - File to delete is %previousdayFile%

so effectively, depending on which file you want to transfer you can then run

echo put %yesterdayFile% >> uploadsp.txt

or

echo put %prevousdayFile% >> uploadsp.txt

Upvotes: 0

Related Questions