删除日期后的所有文件,代码很慢

Delete all files after date, code is slow

我上周提出了一个关于获取批处理文件或代码以删除过去 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命令。