1

我需要一个批处理文件,它将获取给定文件夹中的任何文件,其名称中的值为 YYYYMMDD ,大于今天的日期(对于未来的某个合理范围)并将它们移动到另一个文件夹。

set MainFolder=..\
set FutureFolder=.\
set FutureDaysToCount=90
set today=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%

上面示例中的 %today% 将给我(2012 年 5 月 30 日):20120530

我想做的是从 %today% TO %today% + %FutureDaysToCount%循环日期并执行:

move *yyyymmdd*.txt .\SomeOtherFolder

...对于 90 天范围内的每一天,其中yyyymmdd将是正在处理的当前日期的数值等价物。所以:

move *20120530*.txt .\SomeOtherFolder
move *20120531*.txt .\SomeOtherFolder
move *20120601*.txt .\SomeOtherFolder  REM note new month here!

有没有办法在循环中增加天数来实现这一点?

笔记

这与以下类似,但不同:
将具有 YYYYMMDD 格式的日期的文件从一个文件夹移动到另一个文件夹的批处理过程

4

2 回答 2

2

使用 WMIC 获取今天的日期(无论语言环境如何)。

然后使用http://www.dostips.com/DtTipsDateTime.php上的julian 日期函数在循环中计算您的日期字符串。

@echo off
setlocal enableDelayedExpansion
for /f "skip=1" %%D in ('wmic os get localdatetime') do set dt=%%D&goto :break
:break
call :date2jdate jdStart %dt:~0,4% %dt:~4,2% %dt:~6,2%
set /a jdEnd=jdStart+90
for /l %%N in (%jdStart% 1 %jdEnd%) do (
  call :jdate2date %%N yyyy mm dd
  move "*!yyyy!!mm!!dd!*.txt" ".\SomeOtherFolder"
)
exit /b

:date2jdate JD YYYY MM DD -- converts a gregorian calender date to julian day format
::                        -- JD   [out] - julian days
::                        -- YYYY [in]  - gregorian year, i.e. 2006
::                        -- MM   [in]  - gregorian month, i.e. 12 for december
::                        -- DD   [in]  - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set "yy=%~2"&set "mm=%~3"&set "dd=%~4"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b

:jdate2date JD YYYY MM DD -- converts julian days to gregorian date format
::                     -- JD   [in]  - julian days
::                     -- YYYY [out] - gregorian year, i.e. 2006
::                     -- MM   [out] - gregorian month, i.e. 12 for december
::                     -- DD   [out] - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set /a L= %~1+68569,     N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447,  K= L-2447*J/80,      L= J/11
set /a J= J+2-12*L,      I= 100*(N-49)+I+L
set /a YYYY= I,  MM=100+J,  DD=100+K
set MM=%MM:~-2%
set DD=%DD:~-2%
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" (SET %~2=%YYYY%) ELSE echo.%YYYY%
    IF "%~3" NEQ "" (SET %~3=%MM%) ELSE echo.%MM%
    IF "%~4" NEQ "" (SET %~4=%DD%) ELSE echo.%DD%
)
EXIT /b
于 2012-05-02T01:32:50.767 回答
2

下面的批处理文件做你想做的事:

@echo off
setlocal EnableDelayedExpansion
set FutureDaysToCount=90

rem Get parts of current date and set number of days by month
set year=%DATE:~10,4%
set MM=%DATE:~4,2%
set DD=%DATE:~7,2%
set /A month=1%MM% %% 100, day=1%DD% %% 100
set i=0
for %%d in (31 28 31 30 31 30 31 31 30 31 30 31) do (
   set /A i+=1
   set days[!i!]=%%d
)
if %month% lss 3 (
   rem Check if current year is leap
   set /A leapYear=year %% 4
) else (
   rem Check if next year is leap
   set /A "leapYear=(year+1) %% 4"
)   
if %leapYear% equ 0 set days[2]=29
set daysThisMonth=!days[%month%]!

rem Loop through the dates for the number of days
for /L %%i in (1,1,%FutureDaysToCount%) do (
   rem Advance the date to next day
   set /A day+=1
   if !day! gtr !daysThisMonth! (
      rem Advance the date to next month
      set /A month+=1, day=1
      if !month! gtr 12 (
         rem Advance the date to next year
         set /A year+=1, month=1
      )
      call :setElem daysThisMonth=days[!month!]
      set MM=!month!
      if !month! lss 10 set MM=0!month!
   )
   set DD=!day!
   if !day! lss 10 set DD=0!day!

   rem Execute the desired command:
   ECHO move *!year!!MM!!DD!*.txt SomeOtherFolder
)
goto :EOF

:setElem var=vector[!index!]
set %1=!%2!
exit /B

可以使用本文中描述的方法轻松修改此批处理文件以独立于区域设置日期设置

于 2012-05-02T06:23:09.240 回答