简体   繁体   English

如何获取早于指定日期的文件?

[英]How can I get files older than a specified date?

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. 我希望能够在XP中(使用cmd)查找早于特定日期的文件。 I want to do something like DIR/FIND files that are older than 30 days. 我想执行30天以上的DIR / FIND文件之类的操作。

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! 谢谢!

使用forfiles

forfiles /p "i:\folder1" /m *.pdf /d -30 /c "move /y z:\folder1\@file i:\folder2"

Julian date function are taken from DosTips.com function library 朱利安日期函数取自DosTips.com函数库

First use XCOPY /L /D to get list of files that have been modified within past 30 days: save to an exclude list. 首先使用XCOPY / L / D获取过去30天内已修改的文件列表:保存到排除列表。

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. 然后使用XCOPY / L获取所有文件,并通过FINDSTR进行管道传输,以从排除列表中排除文件,并将结果保存到包含列表中。

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. 我没有使用XCOPY / EXCLUDE选项,因为它会将文件视为具有隐式通配符。 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. 因此,如果您指定*而不是* .pdf,则名为TEST的排除文件也将导致也排除名为TEST1,TEST.TXT和TEST.EXE的文件。

The FINDSTR /I option is required to compensate for a FINDSTR bug when doing a search with multiple literal search strings. 使用多个文字搜索字符串进行搜索时,需要使用FINDSTR / I选项来补偿FINDSTR错误。 The /I option shouldn't be required, but doing a case sensitive search with multiple literal search strings can give the wrong result. / I选项不是必需的,但是使用多个文字搜索字符串进行区分大小写的搜索会产生错误的结果。

See the answer to batch file to delete files older than a specified date for a way to do this with xcopy. 请参阅批处理文件答案以删除早于指定日期的文件,以获取使用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. 使用xcopy时,您确实需要指定完整日期(不能说“ 30天前”),但是可以通过其他命令来计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM