我必须搜索包含文件的特定文件夹*.txt
,但文件的名称包含一些日期,如20120712
,20120713
等等。
我想搜索具有特定日期或更长时间的文件并将它们移动到另一个文件夹。如何使用批处理搜索包含某个日期或更长时间的文件名?
我必须搜索包含文件的特定文件夹*.txt
,但文件的名称包含一些日期,如20120712
,20120713
等等。
我想搜索具有特定日期或更长时间的文件并将它们移动到另一个文件夹。如何使用批处理搜索包含某个日期或更长时间的文件名?
尝试类似:
@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