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

China☆狼群 提交于 2020-01-15 11:34:17

问题


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

回答1:


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"



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/47352095/get-previous-and-day-before-previous-date-in-batch-filecmd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!