问题
I apologize if this question was already asked but I can't seem to find exactly what I am looking for. I want to be able to find files in XP (using cmd) that are older than a specific date. I want to do something like DIR/FIND files that are older than 30 days.
The following does not work but hopefully it will give you an idea of what I am trying to do.
FOR /f %%f IN ('DIR /b /t -30 I:\FOLDER1\*.pdf') DO move /Y Z:\FOLDER1\%%f "I:\FOLDER2\"
Thanks!
回答1:
Julian date function are taken from DosTips.com function library
First use XCOPY /L /D to get list of files that have been modified within past 30 days: save to an exclude list.
Then use XCOPY /L to get all files and pipe through FINDSTR to exclude the files from the exclude list and save results to an include list.
Finally loop through include list and move the files
@echo off
setlocal
set src="I:\FOLDER1\*.pdf"
set dest="I:\FOLDER2"
set incl="%temp%\includeFiles%random%.txt"
set excl="%temp%\excludeFiles%random%.txt"
call :jdate jd
set /a jd-=30
call :jdate2date jd yyyy mm dd
xcopy %src% %dest% /d:%mm%-%dd%-%yyyy% /l | findstr /vxrc:"[0-9]* File(s)" >%excl%
xcopy %src% %dest% /l | findstr /vxrc:"[0-9]* File(s)" | findstr /vixlg:%excl% >%incl%
set /a "fail=0, ok=0"
for /f "usebackq eol=: delims=" %%F in (%incl%) do (
move /y "%%F" %dest% >nul 2>&1 && (
echo "%%F"
set /a "ok+=1"
) || (
>&2 echo ERROR: Unable to move "%%F"
set /a "fail+=1"
)
)
echo ----------------------
echo Moved %ok% files
if %fail% gtr 0 echo Failed to move %fail% files
del %excl%
del %incl%
exit /b %fail%
: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
:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
:: -- JD [out,opt] - julian days
:: -- DateStr [in,opt] - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
:$reference http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20060101 :$changed 20090328 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set DateStr=%~2&if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
set %%a=%%A&set %%b=%%B&set %%c=%%C))
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
I did not use the XCOPY /EXCLUDE option because it treats the files as having implicit wildcards. So if you specified * instead of *.pdf, then an excluded file named TEST would also cause files named TEST1, TEST.TXT, and TEST.EXE to be excluded as well.
The FINDSTR /I option is required to compensate for a FINDSTR bug when doing a search with multiple literal search strings. The /I option shouldn't be required, but doing a case sensitive search with multiple literal search strings can give the wrong result.
回答2:
Use forfiles:
forfiles /p "i:\folder1" /m *.pdf /d -30 /c "move /y z:\folder1\@file i:\folder2"
回答3:
See the answer to batch file to delete files older than a specified date for a way to do this with xcopy.
With xcopy you do need to specify the full date (you can't say "30 days ago"), but you could calculate this via other commands.
来源:https://stackoverflow.com/questions/8827805/how-can-i-get-files-older-than-a-specified-date