8} How do I find all the files made at or after YYYYMMDD HHMM?
Let us start with the easier task of finding and
copying all the files made today. Be forewarned, though. It is slow.
@echo off & setlocal enableextensions
::
set TargetFolder=C:\
Whatever
if not exist "%TargetFolder%" (
echo Target folder "%TargetFolder%" not found
goto :EOF)
::
:: Get the date elements (see FAQ Item #1)
:: The date presentation assumes DD.MM.YYYY (else customize)
for /f "tokens=1-3 delims=./-" %%a in ("%date%") do (
set
dd_=%%a
set
mm_=%%b
set
yyyy_=%%c)
set dd_=0%dd_%
set dd_=%dd_:~-2%
set xcdate_=
%mm_%-%dd_%-%yyyy_%
::
:: Test the copying. When done, remove the test switch /L
:: Customize the target s:\
for /f "tokens=*" %%d in ('dir "C:\*.*" /s /b /a:-d-s-h') do (
xcopy
/L /p /v /f /d:%
xcdate_% "%%~fd" "%TargetFolder%\"
)
endlocal & goto :EOF
Then lets move to the more demanding task of finding the files made
at or after YYYYMMDD HHMM
@echo off & setlocal enableextensions
disabledelayedexpansion
::
:: Where to start from? Some options to customize!
set MyRoot=%USERPROFILE%\My Documents
set MyRoot=C:\_F
if not exist "%MyRoot%\" (
echo Start folder "%MyRoot%" not found
goto :EOF)
::
if not "%1"=="" goto _ask
:: The usage help
:: Note below the need of the escape character (^) because
:: parentheses are special characters
echo.
echo Usage: %~f0 SinceDateAs:YYYYMMDD [SinceTimeAs:HHMN]
echo.
echo For example: %~f0 20031116 0900
echo %~f0 today 0900
echo %~f0 today
^(the time defaults to 00:00
^)
echo.
echo Careful, minimal error checking!
goto :EOF
::
:_ask
echo This might take some time.
:_ask2
set ask_=
set /p ask_="Run anyway [Y/n]?"
if /i "%ask_%"=="n" goto :EOF
if not "%ask_%"=="" if /i not "%ask_%"=="y" goto _ask2
::
:: Get today's date elements
:: The date presentation assumes DD.MM.YYYY (else customize)
set dd_=%date:~0,2%
set mm_=%date:~3,2%
set yyyy_=%date:~6,4%
::
:: Process the user's parameters
set cutdate_=%1
if /i "%cutdate_%"=="today" set cutdate_=%yyyy_%%mm_%%dd_%
if "%2"=="" (
set cutime_=0000
) ELSE (
set cutime_=%2)
::
:: Do some trivial parameter checking (optional)
set ParamError=
if "
%cutdate_:~7,1%"=="" set ParamError=true
echo %cutdate_%|findstr "
[^0-9]">nul
if %errorlevel% EQU 0 set ParamError=true
if "%cutime_:~3,1%"=="" set ParamError=true
echo %cutime_%|findstr "[^0-9]">nul
if %errorlevel% EQU 0 set ParamError=true
if defined ParamError (
echo Error in the parameters %cutdate_% %cutime_%
goto :EOF)
::
:: Traverse the folder trees
if exist "%temp%\TODAY.DIR" del "%temp%\TODAY.DIR"
echo Date Time Size Name
for /f "tokens=*" %%f in (
'dir "%MyRoot%\*.*" /s /b /-c /a:-d-s-h') do (
call :ListFileIfSince "%%~ff"
)
::
:: Display the results optionally redirected to "%temp%\TODAY.DIR"
sort "%temp%\TODAY.DIR"| more
::
:: Delete the results file if you so wish
del /p "%temp%\TODAY.DIR"
::
endlocal & goto :EOF
::
:: ====================================================================
:: Subroutine: Get the file's information and compare the datetimestamp
:ListFileIfSince
setlocal enableextensions
disabledelayedexpansion
set filename_=%1
for %%a in (%filename_%) do (
set fdate_=%%~tf
set fattr_=%%~af
set fsize_=%%~zf
)
call :RightJustify10 %fsize_% fsize_
for /f "tokens=1-5 delims=./-: " %%a in ("%fdate_%") do (
set fdd_=%%a
set fmm_=%%b
set fyyyy_=%%c
set fhh_=%%d
set fmn_=%%e
)
if %fyyyy_%%fmm_%%fdd_% LSS %cutdate_% goto :EOF
if %fyyyy_%%fmm_%%fdd_% EQU %cutdate_% if %fhh_%%fmn_% LSS %cutime_% goto :EOF
>>"%temp%\TODAY.DIR"
echo %fyyyy_%%fmm_%%fdd_% %fhh_%%fmn_% %fsize_% %filename_%
endlocal & goto :EOF
::
:: =========================================================
:: Subroutine: Pad a string to always be ten characters long
:RightJustify10 number_ return_
setlocal enableextensions disabledelayedexpansion
set number_= %1
set return_=%number_:~-10%
endlocal & set "%~2=%return_%" & goto :EOF
It is easy to see that by suitably customizing the last "
echo" line you could perform many kinds of commands
on those files. Or you could first direct the output as a safety to
yet another script file for further inspection and editing.
And so on.
The output might look something like this
C:\_D\TEST>cmdfaq today 1000
Date Time Size Name
20080407 1020 1119035 "C:\_F\HAKEM\GARF\GARF_.ZIP"
20080407 1023 1166358 "C:\_F\HAKEM\BERT\BERT_.ZIP"
20080407 1048 970 "C:\_F\FHELP\TOBEDONE.TXT"
20080407 1157 11115 "C:\_F\INF\TSCMDIDX.TXT"
20080407 1157 33342 "C:\_F\INF\TSCMDNWS.TXT"
20080407 2101 426766 "C:\_F\CMD\1CMDFAQ.TXT"
C:\DOCUME~1\TS8C97~1.HUP\LOCALS~1\Temp\TODAY.DIR, Delete (Y/N)? y
One more thing. Consider the snippet
for /f "tokens=*" %%f in (
'dir "%MyRoot%\*.*" /s /b /-c /a:-d-s-h') do (
call :ListFileIfSince "%%~ff"
)
You might wish to browse a different set of folders. E.g. I might have
for /f "tokens=*" %%f in (
'dir "C:\_D\*.*"
"C:\_E\*.*"
"C:\_F\*.*"
"C:\_G\*.*"
"C:\_H\*.*"
"C:\_L\*.*"
/s /b /-c /a:-d-s-h') do (
call :ListFileIfSince "%%~ff"
)
Also see
RECENT.CMD and RECENT.VBS
scripts included in the tscmd.zip package.
There is a special command
FORFILES which
also could be used to identify files made at and after a specified
date in a folder. For example
@echo off & setlocal enableextensions
forfiles -p"C:\_D\TEST" -m"*.*" -d
+12042009 -c"cmd /c echo @FDATE @PATH\@FILE"
endlocal & goto :EOF
The output might be e.g.
C:\>C:\_D\TEST\CMDFAQ.CMD
20120115 C:\_D\TEST\CMDFAQ.CMD
20111206 C:\_D\TEST\CMDFAQ2.CMD
20111010 C:\_D\TEST\My test file.txt
.txt was unexpected at this time. [forfiles is intolerant of the potential poison characters]
20100708 C:\_D\TEST\VBSFAQ.VBS
20090412 C:\_D\TEST\VBSFAQ2.VBS
Similarly, one might want to know which files in a folder were last
modified a hundred or more days ago:
@echo off & setlocal enableextensions
forfiles -p"C:\_D\TEST" -m"*.*" -d
-100 -c"cmd /c echo @FDATE @PATH\@FILE"
endlocal & goto :EOF
The output might be e.g.
C:\>C:\_D\TEST\CMDFAQ.CMD|sort
20060520 C:\_D\TEST\BATFAQ2.BAT
20061010 C:\_D\TEST\BATFAQ.BAT
20071226 C:\_D\TEST\TEST.CMD
20080407 C:\_D\TEST\test
20080408 C:\_D\TEST\test (4!).txt
20080427 C:\_D\TEST\My test file2.txt
20090216 C:\_D\TEST\broken.zip
20090216 C:\_D\TEST\good.zip
20090412 C:\_D\TEST\VBSFAQ2.VBS
20100708 C:\_D\TEST\VBSFAQ.VBS