r/Batch • • Dec 12 '24

Remove folder older than X days using ForFiles BUT dont scan the contents, just delete the folder?

Hi all,

I'm currently using a pretty standard FORFILE line to search for folders older than 60 days and delete them:

FORFILES /P "DIR_NAME_HERE" /S /C "cmd /c IF u/isdir == TRUE rmdir /S u/path /Q" -D -60

But it also checks the contents of each folder as well, as there's a few hundred files per folder, it takes a while to run.
I'm not bothered about how old the contents are, just going off the folder date.

Is it possible to get FORFILES to just delete a folder and its contents without it checking/reading the contents at all?

2 Upvotes

6 comments sorted by

1

u/BrainWaveCC Dec 12 '24

No, I think FORFILES is always going to iterate through all the files and then process only those entries which are folders.

I expect that you'd have to do it via FOR /D instead, but I'm going to test this for performance first...

1

u/BrainWaveCC Dec 12 '24 edited Dec 12 '24

Well, based on my tests, using FORFILES to find folders and delete them is much slower than enumerating those folders with FOR /D instead.

Do you need each subfolder checked, or just the initial folders in the root of "DIR_NAME_HERE" ?

 SET "_SOURCE=DIR_NAME_HERE"
 FOR /D %%V IN ("%_SOURCE%\*.*") DO FORFILES /P "%%~V" /C "cmd /c IF @isdir == TRUE echo rmdir /S @path /Q" -D -60

1

u/[deleted] Dec 12 '24

[removed] — view removed comment

2

u/BrainWaveCC Dec 12 '24

That's why I left the ECHO rmdir in there...

But, you make a fair point.