print files that are created in last 5days(winodws XP)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
93905
Posts: 3
Joined: 29 Sep 2011 06:58

print files that are created in last 5days(winodws XP)

#1 Post by 93905 » 29 Sep 2011 11:07

Hi Experts,

I want to print all the files in a directory that are created/accessed/modified in less than (<=)5 days. and i want to redirect this output to a file.

Urgent in need
Please help.
Thanks in advance.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: print files that are created in last 5days(winodws XP)

#2 Post by dbenham » 29 Sep 2011 13:09

Do you want to print the contents, or do you want to print the file names?

Do you really want to include files that were accessed but not changed within the last 5 days?

Dave Benham

93905
Posts: 3
Joined: 29 Sep 2011 06:58

Re: print files that are created in last 5days(winodws XP)

#3 Post by 93905 » 30 Sep 2011 00:30

I want to print only the names of the files into a file. the files that are created/modified in last 5 days.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: print files that are created in last 5days(winodws XP)

#4 Post by dbenham » 30 Sep 2011 08:53

If you can lay your hands on ROBOCOPY, the solution is very simple and quick. It is available for XP within the Windows Server 2003 Resource Kit Tools

Code: Select all

@echo off
setlocal
set myPath="c:\."
set outFile="changedFiles.txt"
robocopy %myPath% %myPath% /l /maxage:5 /xx /is /it /ns /nc /ndl /np /njh /njs >%outFile%
The output has a bunch of leading spaces before each file.

If you want to eliminate the leading spaces, then

Code: Select all

@echo off
setlocal
set myPath="c:\."
set outFile="changedFiles.txt"
(
for /f "tokens=*" %%F in ('robocopy %myPath% %myPath% /l /maxage^:5 /xx /is /it /ns /nc /ndl /np /njh /njs') do @echo %%F
)>%outFile%


If you don't have ROBOCOPY then there is a more complicated and slower solution using the DOSTIPS :jdate function

Code: Select all

@echo off
setlocal enableDelayedExpansion
set myPath="c:\*"
set outFile="changedFiles.txt"
call :jdate today
(
  for %%F in (!myPath!) do (
    call :jdate fileDate %%~tF
    set /a "fileAge=today-fileDate"
    if !fileAge! leq 5 echo %%F
  )
)>%outFile%
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

Note that the ROBOCOPY solutions include hidden and system files as written. The :jdate function version does not.

Dave Benham

Post Reply