0

我必须搜索包含文件的特定文件夹*.txt,但文件的名称包含一些日期,如2012071220120713等等。

我想搜索具有特定日期或更长时间的文件并将它们移动到另一个文件夹。如何使用批处理搜索包含某个日期或更长时间的文件名?

4

1 回答 1

1

尝试类似:

@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
于 2012-07-27T21:55:43.433 回答