1

我有一个批处理文件,它返回日期减去给定天数。

例如:如果我们是 09/11/2013batch.bat today -1 将返回 08/11/2013

如果是周末,它将返回星期五日期。

问题是我试图让批次给我最后一个工作日而不输入任何参数,但我失败了

这是代码:

@echo off
if "%~2"=="" (
echo to get yesterdays date use     call "%~n0" today -1
echo.
echo Add a third parameter if you want a separator in the date string
echo EG: for this format YYYY-MM-DD using yesterdays date
echo     call "%~n0" today -1 -
echo.
pause
goto :EOF)

set date1=%1
set qty=%2
set separator=%~3
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)^&_
echo>>"%temp%\%~n0.vbs"         d
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "YY=%result:~0,4%" & set "MM=%result:~4,2%" & set "DD=%result:~6,2%" & set "daynum=%result:~-1%"
:: if the daynum is a weekend then run the batch file again to get the friday
set "weekend="
if %daynum% EQU 1 set /a weekend=qty - 2
if %daynum% EQU 7 set /a weekend=qty - 1
if defined weekend %0 %1 %weekend%

set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
echo %%YY%% is set to %YY%
echo %%MM%% is set to %MM%
echo %%DD%% is set to %DD%
echo.
echo daynum is "%daynum%"
echo date is %YY%%MM%%DD%
4

1 回答 1

0
@echo off
set qty=-1
:loop4weekends
set "separator="
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,now)
echo>>"%temp%\%~n0.vbs" d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)^&_
echo>>"%temp%\%~n0.vbs"         d
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "YY=%result:~0,4%" & set "MM=%result:~4,2%" & set "DD=%result:~6,2%" & set "daynum=%result:~-1%"
:: if the daynum is a weekend then loop to get the friday
set "weekend="
if %daynum% EQU 1 set weekend=1&set "qty=-3"
if %daynum% EQU 7 set weekend=1&set "qty=-2"
if defined weekend goto :loop4weekends

set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
于 2013-11-12T08:47:36.717 回答