How can I get files older than a specified date?

11,992

Solution 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.

Solution 2

Use forfiles:

forfiles /p "i:\folder1" /m *.pdf /d -30 /c "move /y z:\folder1\@file i:\folder2"
Share:
11,992
northpole
Author by

northpole

just a regular old programmer

Updated on June 04, 2022

Comments

  • northpole
    northpole almost 2 years

    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!

  • northpole
    northpole over 12 years
    forfiles does not work on XP (well at the very least it is not a valid command), I should have mentioned that.
  • Waihon Yew
    Waihon Yew over 12 years
    @northpole: Did you try downloading a copy e.g. from petri.co.il/download_free_reskit_tools.htm and running it?
  • northpole
    northpole over 12 years
    no, I did, however, grab rktools and can make it work using robocopy. But I was hoping to be able to do this using commands that come installed already so that I don't have to download and install on each of the PCs that are going to use this.
  • Mr Moose
    Mr Moose over 12 years
    I was going to suggest Robocopy as a potential solution if your request was related to copy/moving files. It comes with Windows 7 by default now, so if ever move to Win7, you'll be fine.
  • dbenham
    dbenham over 12 years
    Edit - Added logging info and compensated for a FINDSTR bug
  • northpole
    northpole over 12 years
    excellent, I was finally able to review this and it will work out. Thanks for the effort!!!
  • boto
    boto over 12 years
    If using Win XP external utilies are an option then I would suggest to use the ported 'find' command in UnixUtils package (see unxutils.sourceforge.net), you would need to copy the find.exe only. 'find' provides some extended options to find files basing on their last access/modification (see the -ctime option).