我需要一个批处理文件,它将获取给定文件夹中的任何文件,其名称中的值为YYYYMMDD ,大于今天的日期(对于未来的某个合理范围)并将其移至另一个文件夹。
set MainFolder=..\
set FutureFolder=.\
set FutureDaysToCount=90
set today=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
%今天%在上面的例子中将给我(2012年5月30日):20120530
我想要做的是从今天的%%到今天的%TO%循环日期%+%FutureDaysToCount%并执行:
move *yyyymmdd*.txt .\SomeOtherFolder
...对于90天范围内的每一天, yyyymmdd 将是当前处理日期的数字等效值。所以:
move *20120530*.txt .\SomeOtherFolder
move *20120531*.txt .\SomeOtherFolder
move *20120601*.txt .\SomeOtherFolder REM note new month here!
有没有办法在循环中增加天数来实现这一目标?
这是类似的,但不同于:
Batch process to move file having Date in YYYYMMDD format from one folder to another folder
答案 0 :(得分: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
答案 1 :(得分: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
中描述的方法,可以轻松修改此批处理文件,使其独立于区域设置日期设置