Board logo

标题: [求助]这个获取“昨天”的bat文件有否改进的空间? [打印本页]

作者: ShingU     时间: 2008-11-16 18:28    标题: [求助]这个获取“昨天”的bat文件有否改进的空间?


@echo off
FOR /F "TOKENS=1-3 DELIMS=- " %%A IN ('echo %DATE%') do (set Year=%%A&SET Month=%%B&SET Day=%%C)
FOR %%A IN (%Year%)  DO SET Year=%%A
FOR %%A IN (%Month%) DO SET Month=%%A
FOR %%A IN (%Day%)   DO SET Day=%%A
SET SortDate=%Year%%Month%%Day%

FOR %%A IN (%Date%) DO SET Today=%%A
SET DayS=%Day%

IF %DayS% EQU 1 (
SET YesterY=%Year%
CALL :RollMonth
) ELSE (
SET /A YesterD=%DayS%-1
SET YesterM=%Month%
SET YesterY=%Year%
)

IF %YesterD% LSS 10 SET YesterD=0%YesterD%
SET SortYest=%YesterY%%YesterM%%YesterD%

REM CALL ECHO %SortYest%
ENDLOCAL
GOTO:EOF
:RollMonth
IF %Month%==01 (
SET YesterD=31
SET YesterM=12
SET /A YesterY = %Year% - 1
)
IF %Month%==02 (
SET YesterD=31
SET YesterM=01
)
IF %Month%==03 (
SET YesterD=28
SET YesterM=02
CALL :LeapYear
)
IF %Month%==04 (
SET YesterD=31
SET YesterM=03
)
IF %Month%==05 (
SET YesterD=30
SET YesterM=04
)
IF %Month%==06 (
SET YesterD=31
SET YesterM=05
)
IF %Month%==07 (
SET YesterD=30
SET YesterM=06
)
IF %Month%==08 (
SET YesterD=31
SET YesterM=07
)
IF %Month%==09 (
SET YesterD=31
SET YesterM=08
)
IF %Month%==10 (
SET YesterD=30
SET YesterM=09
)
IF %Month%==11 (
SET YesterD=31
SET YesterM=10
)
IF %Month%==12 (
SET YesterD=30
SET YesterM=11
)
GOTO:EOF

:LeapYear
SET /A mod400 = %Year% %% 400
SET /A mod100 = %Year% %% 100
SET /A mod4   = %Year% %% 4
IF %mod400% EQU 0 (
SET YesterD=29
) ELSE (
IF %mod100% EQU 0 (
  SET YesterD=28
) ELSE (
  IF %mod4% EQU 0 (
   SET YesterD=29
  ) ELSE (
   SET YesterD=28
  )
)
)
GOTO:EOF
:Syntax
ECHO PREV: Get yesterday

作者: HAT     时间: 2008-11-16 18:37
参考这个算法:http://www.cn-dos.net/forum/viewthread.php?tid=30251#pid198201