2017-04-18 31 views
0

我在該文件夾中有大約20 000個文件,我想壓縮並刪除超過7天的文件。我試過這個腳本,但它工作得很慢:使用7ZIP和CMD壓縮並刪除7天以前的文件

Set TDate=%date:~6,4%%date:~3,2%%date:~0,2% 

for /f "delims=" %%i in (' 
forfiles /p C:\ARCHIVE /s /m *.txt /d -7 /c "cmd /c echo @path" 
') do (
"%ProgramFiles%\7-Zip\7z.exe" a "C:\ARCHIVE_%TDate%.zip" %%i 
del /a /f %%i 
) 

請指教如何使它工作更快。

+3

這個問題屬於上的[代碼審查(https://開頭codereview.stackexchange.com/)網站。 – aschipfl

回答

2

另外的forfiles使用,這是非常慢的(這個劇本,但不可避免的,我認爲),你的腳本的主減速部分是存檔的,在每一個循環迭代修改。相反,你應該做的歸檔只有一次,也許使用一個列表文件,然後讓歸檔工具刪除文件,它成功地壓縮自身:

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

rem // Define constants here: 
set "_ROOT=C:\ARCHIVE" 
set "_PATTERN=*.txt" 
set "_LIST=%TEMP%\%~n0.tmp" 
set "_ARCHIVER=%ProgramFiles%\7-Zip\7z.exe" 

rem // Get current date in locale-independent format: 
for /F "tokens=2 delims==" %%D in ('wmic OS get LocalDateTime /VALUE') do set "TDATE=%%D" 
set "TDATE=%TDATE:~,8%" 

rem // Create a list file containing all files to move to the archive: 
> "%_LIST%" (
    for /F "delims=" %%F in (' 
     forfiles /S /P "%_ROOT%" /M "%_PATTERN%" /D -7 /C "cmd /C echo @path" 
    ') do echo(%%~F 
) && (
    rem // Archive all listed files at once and delete the processed files finally: 
    "%_ARCHIVER%" a -sdel "%_ROOT%_%TDATE%.zip" @"%_LIST%" 
    rem // Delete the list file: 
    del "%_LIST%" 
) 

endlocal 
exit /B 
+0

謝謝,它工作得更快(2分鐘而不是7小時) –

相關問題