我创建了一个批处理脚本,用于查找上个月的文件(例如,如果当前月份为11月,则会查找10月份的文件)。 它需要查找具有超过75000个文件的文件的源。所以我的脚本遍历每个文件,然后检查它是否来自上个月。 如果文件来自上个月,它将它们移动到临时文件夹,然后它正在压缩该临时文件夹。
怀疑:由于脚本需要遍历~75000个文件,因此需要花费大量时间。脚本是否有任何方法可以查找仅来自上个月的文件。它不会触及当月的文件。
我在下面给出了代码: echo off
SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
REM this scripts that compress the files in a folder
REM then it move those files on another location
set src_dir=%1%
set back_up=%2%
set save_file=%3%
set ZipTool=%4%
echo Verify System date
echo -------------------
for /f "skip=2 tokens=2-3 delims=," %%A in ('WMIC Path Win32_LocalTime Get Month^,Year /Format:csv') do (
set /a "mm=%%A"
set /a "yyyy=%%B"
)
set err=%errorlevel%
if not %err%==0 goto Date_error
set /a pre_month=%mm%-1
if %mm% EQU 1 (
set mm=12
set /a yyyy-=1
) else (
set /a mm-=1
)
REM Zero pad month if needed
if %mm% LSS 10 set "mm=0%mm%"
if %pre_month% LSS 10 set "pre_month=0%pre_month%"
echo Prev_Month in yyyymm formate is: %yyyy%%mm%
REM creating TEMP folder
set temp=%back_up%\temp\%yyyy%%pre_month%
REM Creating Temporary Folder
mkdir %temp%
echo %temp% created...
set /a count=0
REM moving files into TEMP Folder
for %%A in ("%src_dir%\*.*") do (
REM Get month and year from this file
set "FileTime=%%~tA"
set "FileYYYYMM=!FileTime:~6,4!!FileTime:~,2!"
::echo !FileYYYYMM! and %yyyy%%mm%>nul
REM If it is from a current Month, move it to temp.
if "%yyyy%%mm%" EQU "!FileYYYYMM!" if !count! LSS 10000 (
set /a count=count+1
move %%~fA %temp% >nul ) )
set err=%errorlevel%
if not %err%==0 goto Error
echo Starting 7 Zip Process..
echo --------------------------
%ZipTool% a -mx=9 %back_up%\%yyyy%-%pre_month%.zip %tmp%\*.* -ssw > nul
set err=%errorlevel%
if not %err%==0 goto Error
echo Compresed Files .... >%save_file%
echo --------------------------->>%save_file%
for %%G in ("%tmp%\*.*") do echo %%~nG >>%save_file%
echo ---------------------------->>%save_file%
if exist %save_file% (
call filesize %save_file% 0
echo %save_file% has been attached
)
pushd %back_up%\temp
rd /q /s %yyyy%%pre_month%
popd
set err=10
goto END
:Date_error
echo -----------------------
echo ERROR:
echo Unable to fetch System Date.
echo -----------------------
set err=20
GOTO END
:FolderError
echo -----------------------
echo ERROR:
echo %yyyy%%pre_month% doesn't exist.Please verify.
echo -----------------------
set err=20
GOTO END
:Error
echo -----------------------
echo ERROR:
echo Script Execution Error..Please verify.
echo -----------------------
set err=20
GOTO END
:PROMPTS
echo -----------------------
echo Prompt Error:
echo Prompt missing from the job..Please verify.
echo -----------------------
set err=20
GOTO END
:END
echo+
echo Bat Return Code: %err%
echo+
ECHO == END process ==
echo+
exit /b %err%
答案 0 :(得分:0)
假设:
SRC_DIR
包含源目录的完整路径TMP_DIR
包含临时目录的完整路径YYYY/MM
以给定格式(4位数年份,/
,2位数月份)保存上个月的年份和月份,YYYY/MM/DD
(4位数年份,/
,2位数月份,/
,2位数日期,以下内容适用于您:
setlocal EnableExtensions EnableDelayedExpansion
set "FLAG="
for /F "eol=| delims=" %%I in ('
dir /B /A:-D /O:-D "%SRC_DIR%"
') do (
set "ITEM=%%~tI"
if "!ITEM:~,7!"=="%YYYY/MM%" (
ECHO move "%%~fI" "%TMP_DIR%"
set "FLAG=QUIT"
) else (
if defined FLAG goto :QUIT
)
)
:QUIT
endlocal
这会将受影响的文件回显到屏幕上。如果输出正确,请删除ECHO
。
如果您的系统日期格式为DD/MM/YYYY
,请将if "!ITEM:~,7!"=="%YYYY/MM%"
替换为if "!ITEM:~6,4!!ITEM:~2,3!"=="%YYYY/MM%"
。
如果您的系统日期格式为MM/DD/YYYY
,请将if "!ITEM:~,7!"=="%YYYY/MM%"
替换为if "!ITEM:~6,4!/!ITEM:~,2!"=="%YYYY/MM%"
。
答案 1 :(得分:0)
@echo off
setlocal EnableDelayedExpansion
rem Get previous month in YYYYMM format. The line below assume that the date format is "MM/DD/YYYY",
rem so you must modify the "%%a %%c" tokens accordingly if your locale is different: a b c
for /F "tokens=1-3 delims=/" %%a in ("%date%") do set /A MM=1%%a-1, YYYY=%%c
if %MM% equ 100 set /A MM+=12, YYYY-=1
set "prevMonth=%YYYY%%MM:~1%"
rem Process the files *in date order* from newest to oldest
cd "%src_dir%"
for /F "delims=" %%f in ('dir /O:-D /B') do for /F "tokens=1-3 delims=/ " %%a in ("%%~Tf") do (
rem The same note for "date" above apply in next line
set "fileYYYYMM=%%c%%a"
if !fileYYYYMM! gtr %prevMonth% (
echo File "%%f" is of current month...
) else if !fileYYYYMM! equ %prevMonth% (
echo File "%%f" is from previous month...
) else (
echo File "%%f" is the first one two months old or older...
goto breakProcess
)
)
:breakProcess