簡體   English   中英

使用7ZIP和CMD壓縮和刪除7天以上的文件

[英]ZIP and delete files older than 7 days using 7ZIP and CMD

我的文件夾中大約有20000個文件,我想壓縮和刪除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
) 

請告知如何使其更快地工作。

除了使用非常慢的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM