如何获取早于指定日期的文件? [英] How can I get files older than a specified date?

查看:86
本文介绍了如何获取早于指定日期的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉是否已经提出了这个问题,但我似乎找不到确切的答案.我希望能够在XP中(使用cmd)查找早于特定日期的文件.我想执行30天以上的DIR/FIND文件之类的操作.

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\"

谢谢!

推荐答案

Julian日期函数来自 DosTips. com功能库

Julian date function are taken from DosTips.com function library

首先使用XCOPY/L/D获取过去30天内已修改的文件列表:保存到排除列表中.

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

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

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

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.

使用多个文字搜索字符串进行搜索时,需要使用FINDSTR/I选项来补偿FINDSTR错误. /I选项不是必需的,但是使用多个文字搜索字符串进行区分大小写的搜索可能会得出错误的结果.

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.

这篇关于如何获取早于指定日期的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆