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

时间:2015-10-26 21:24:20

标签: date batch-file time cmd delete-file

我上周提出了一个问题,要求获取一个批处理文件或代码来删除在过去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

1 个答案:

答案 0 :(得分:1)

您可以尝试使用

@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命令。