Reputation: 5442
I have to search a particular folder having *.txt
files, but the name of the file contains some dates like 20120712
, 20120713
so on.
I would like to search for files having particular dates or greater and move them to another folder. How can I search for the filename containing a certain date or its greater using batch?
Upvotes: 0
Views: 1580
Reputation: 2229
Try something like:
@echo off
setlocal
set SEARCH_DIR=c:\temp\source
set TARGET_DIR=c:\temp\target
set DATE_THRESHOLD=20120720
REM ***
REM *** NEED TO MODIFY REGEX ACCORDINGLY (THIS REGEX EXPECTS THE FILES TO BE CALLED YYYYMMDD.TXT).
REM ***
for /f %%F in ('dir /b %SEARCH_DIR%\*.txt 2^>nul ^| %SystemRoot%\System32\findstr.exe /r /c:"^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.txt"') do (
call :PROCESS_FILE "%%F"
)
endlocal
goto END
:PROCESS_FILE
set PF_FILENAME=%1
set PF_FILENAME=%PF_FILENAME:"=%
REM ***
REM *** EXTRACT THE NUMERIC PART FROM THE FILENAME. THIS WILL NEED MODIFYING IF YOUR FILENAME FORMAT IS NOT YYYYMMDD.TXT.
REM ***
set /a PF_YYYYMMDD=%PF_FILENAME:~0,8%
echo Processing file [%PF_FILENAME%]...
REM ***
REM *** CHECK DATE.
REM ***
if %PF_YYYYMMDD% LEQ %DATE_THRESHOLD% (
echo File not old enough - skipping.
goto END
)
REM ***
REM *** MOVE FILE.
REM ***
echo Moving file....
move "%SEARCH_DIR%\%PF_FILENAME%" "%TARGET_DIR%"
if exist "%SEARCH_DIR%\%PF_FILENAME%" (
echo ERROR : Failed to move file.
) else (
echo File moved successfully.
)
goto END
:END
Upvotes: 1