上周我提出了一个关于获取批处理文件或代码来删除过去 60 天之前创建的文件夹中的所有 .txt 文件的问题,我被指示使用下面的代码。
forfiles -p "J:\Test_Files" -s -m *.txt* -d 60 -c "cmd /c del @path"
这段代码完成了这项工作并且工作正常,但每分钟删除 250 个文件的速度太慢。我总共需要删除 2,600,000 个文件,这会花费很长时间。
我下面使用的代码每秒删除 200 个文件,但删除所有 .txt 文件
cd /BASE_PATH
del /s *.txt
如何编辑此代码以删除 60 天之前创建的文件?我需要它以更快的速度删除。
感谢您的帮助! :D
最佳答案
你可以尝试一下
@echo off
setlocal enableextensions disabledelayedexpansion
rem Configure script
set "target=J:\Test_Files"
set "fileMask=*.txt"
set "age=60"
rem We will use a temporary file.
for %%t in ("%temp%\%~nx0.%random%%random%%random%.tmp") do (
rem Send to temp file the list of matching files retrieved by the robocopy command
>"%%~ft" robocopy "%target%." "%target%." "%fileMask%" /minage:%age% /l /nocopy /is /s /njh /njs /ndl /nc /ns
rem Process temporary file deleting the selected files
for /f "usebackq tokens=*" %%f in ("%%~ft") do echo del "%%~ff"
rem Once done, remove the temporary file
) & del /q "%%~ft"
del
命令仅回显到控制台。如果输出正确,请删除 echo
命令。
关于date - 删除日期后的所有文件,代码很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33355900/