编辑批处理文件以直接从代码中获取参数



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

例如:如果我们是 09/11/2013 batch.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%
@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)

最新更新