2

I made a question last week about getting a batch file or code to delete all .txt files in a folder that were created before the last 60 days and I was directed to use the code below.

forfiles -p "J:\Test_Files" -s -m *.txt* -d 60 -c "cmd /c del @path"

This code does the job and works fine but it goes too slow deleting 250 files per minute. I need to delete a total of 2,600,000 files and this would take too long.

The code I used below deletes 200 files a second but deletes all .txt files

cd /BASE_PATH
del /s *.txt

How can I edit this code to delete files created before 60 days? I need it to delete at a faster pace.

Thank you for helping! :D

TevinTwoTimes
  • 103
  • 1
  • 10
  • 2
    There are lots of answers that do not use forfiles in these questions: http://stackoverflow.com/questions/324267/batch-file-to-delete-files-older-than-a-specified-date http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days – Jerry Jeremiah Oct 26 '15 at 21:36
  • Would love to see some timed results on all of this. Of course speed is relative to the environment you are working with. – Squashman Oct 27 '15 at 00:50
  • Your last post said 1.4 Million. You have almost doubled the amount you need to delete. How many files do you estimate it has to check if the file is older than X amount of days? – Squashman Oct 27 '15 at 01:01
  • Er, don't you have to specify `-d -60`? Doesn't your code as presented select, and therefore delete, files *newer* than 60 days? – Bacon Bits Oct 27 '15 at 02:39
  • I made an error in the post. It is actually -60 instead of 60. Also my last post was for a different file folder where I just needed to delete all of the .txt files. In this one it has to be before the last 60 days causing the issue I am having. – TevinTwoTimes Oct 27 '15 at 14:49

1 Answers1

2

You can try with

@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 commands are only echoed to console. If the output is correct, remove the echo command.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Yes! This works! Thank you for this, it mass deletes much faster than forfiles code – TevinTwoTimes Oct 27 '15 at 14:55
  • @MC ND, is there any reason why you chose to put the temporary filename into a FOR token instead of just assigning it directly to an environmental variable? I like your use of Robocopy. I have been doing something very similar to find files larger than X amount of bytes. It is a million times faster then checking %~zI using the FOR command. – Squashman Oct 27 '15 at 20:02
  • @Squashman, just a bad habit. I find it easier to read, visually delimited by the `for` construct, and easier to write referencing the temp file via the `for` replaceable parameter, and I have the benefits of using a variable but with less characters. And I have to agree, `robocopy` and `xcopy` are two wrenches that when needed nail nails nicely – MC ND Oct 27 '15 at 20:28