我正在创建一个批处理文件来执行一些exe文件。在该 exe 中,我需要指定上个月的第一个日期(mm/dd/yyyy)和最后一天(mm/dd/yyyy)。
@echo off
start "Testing" "c:\Program Files\app-cmd\bin\admincmd\imagelist -d 08/01/2015 -e 01/09/2015 > c:\test.txt"
Imagelist是一个exe文件,我需要在-d和-e参数中传递值。 我将在计划任务中添加此脚本,以便它将按计划运行。
我无法使用powershell,因为机器是win server 2003。
请指教。
假设您系统上的日期格式为 MM/DD/YYYY,如
date /t
命令所示:
@echo off
setlocal enableDelayedExpansion
for /f "delims=/ tokens=1,3" %%a in ("%date%") do set month=%%a& set year=%%b
if %month:~0,1%==0 set month=%month:~1%
set /a month-=1 && if !month!==0 set month=12&set /a year-=1
if not !month!==2 (
set /a last_day="31 - (month - 1) %% 7 %% 2"
) else (
set /a y4="year %% 4" & if !y4!==0 (
set /a y100="year %% 100" & if not !y100!==0 set is_leap_year=1
set /a y400="year %% 400" & if !y400!==0 set is_leap_year=1
)
if "!is_leap_year!"=="1" (set last_day=29) else set last_day=28
)
set month=0!month!
start "Testing" "c:\Program Files\app-cmd\bin\admincmd\imagelist" ^
-d !month:~-2!/01/!year! -e !month:~-2!/!last_day!/!year! > c:\test.txt
代码将闰年检测为
(year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))
表示二月,并使用 31 - (month - 1) % 7 % 2
表示其他月份。
你可以尝试一下
@echo off
setlocal enableextensions
call :getLastMonthLimits start end
echo %start% - %end%
goto :eof
:getLastMonthLimits startDateReturnVar endDateReturnVar
setlocal enableextensions disabledelayedexpansion
for %%t in ("%temp%\%~nx0.%random%%random%%random%.rpt") do (
for /f "tokens=4,7" %%a in ('
^>nul makecab /f nul /d RptFileName^="%%~ft" /V0
^& type "%%~ft" ^| find "MakeCAB Report:"
') do (
set /a "jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12"
set /a "m=(%%a+10) %% 12 + 1", "y=%%b-m/12", "d=30+((m+m/8) %% 2)", "m+=100"
if /i %%a==mar set /a "d=d-2 +(3-y %% 4)/3 -(99-y %% 100)/99 +(399-y %% 400)/399"
set /a "d+=100"
)
) & del "%%~ft"
endlocal & set "%~1=%m:~-2%/01/%y%" & set "%~2=%m:~-2%/%d:~-2%/%y%"
goto :eof
这使用 carlos 的 getDate 函数的简化版本来获取与语言环境无关的格式的日期(我更喜欢使用
robocopy
,但它不是 2003 年的原生工具)。其余的只是算术运算来计算月份限制。
编辑对于
robocopy
版本(而不是makecab
以独立于区域设置的格式检索日期),以防万一有人需要它
@echo off
call :getLastMonthLimits start end
echo %start% - %end%
goto :eof
:getLastMonthLimits startDateReturnVar endDateReturnVar
setlocal enableextensions disabledelayedexpansion
for /f "tokens=1-2 delims=/ " %%a in ('robocopy "|" . /njh ^| find "|"') do (
set /a "m=((1%%b-100)+10) %% 12 + 1", "y=%%a-m/12", "d=30+((m+m/8) %% 2)", "m+=100"
if %%b==03 set /a "d=d-2 +(3-y %% 4)/3 -(99-y %% 100)/99 +(399-y %% 400)/399"
set /a "d+=100"
)
endlocal & set "%~1=%m:~-2%/01/%y%" & set "%~2=%m:~-2%/%d:~-2%/%y%"
goto :eof
@echo off
setlocal enableDelayedExpansion
for /f "delims=/ tokens=1,2,3" %%a in ("%date%") do (
set month=%%a
set day=%%b
set year=%%c
)
if %month:~0,1%==0 set month=%month:~1%
set /a month-=1 && if !month!==0 set month=12&set /a year-=1
if not !month!==2 (
set /a last_day="31 - (month - 1) %% 7 %% 2"
) else (
set /a y4="year %% 4" & if !y4!==0 (
set /a y100="year %% 100" & if not !y100!==0 set is_leap_year=1
set /a y400="year %% 400" & if !y400!==0 set is_leap_year=1
)
if "!is_leap_year!"=="1" (set last_day=29) else set last_day=28
)
::echo %date%
::echo %day%
::echo %month%
::echo %year%
::echo %last_day%
::pause
set month=0!month!
start "Testing" "c:\Program Files\app-cmd\bin\admincmd\imagelist" ^
-d !month:~-2!/01/!year! -e !month:~-2!/!last_day!/!year! > c:\test.txt
如果日期分隔符不是 / 字符而是 .字符然后使用:
...
for /f "delims=. tokens=1,2,3" %%a in ("%date%") do (
set day=%%a
set month=%%b
set year=%%c
)
...