Batch: Deleting Older FOLDERS
Eizekiel
-
brucine Posted messages 24650 Registration date Status Member Last intervention -
brucine Posted messages 24650 Registration date Status Member Last intervention -
Hello!
I'm reaching out because I want to know the script to put in a batch file to delete any folder older than 30 days on my computer.
Just so you know, I've searched Google for I don't know how long to find this miracle, but all I found were batch scripts for deleting files, not folders.
I already have a batch file cleaner for Temp files and I'm satisfied with it; now I want to get rid of all the old and useless folders along with their contents =)
Thank you in advance.
1 answer
hello
try the Microsoft command forfiles
https://learn.microsoft.com/fr-fr/windows-server/administration/windows-commands/forfiles
Hello,
The behavior of both FORFILES and the alternative ROBOCOPY is misleading: let's suppose I have a folder on the desktop with a creation date of 10/20/2022, which will be the one taken into account and not modified by a subfolder dated 11/06/2022 that I added, containing only files older than 3 months and several files at the root, all of which except one are older than one month.
The syntax:
fails because it correctly selects the old subfolder but, since it only targets directories (@isdir==TRUE), does not return the old files at the root.
Under the same conditions, a ROBOCOPY syntax that I can refine to remove screen junk, but it doesn't matter, selects both the parent folder and the subfolder, and thus deletes everything including the "recent" file:
The moral of the story is that it is ineffective or dangerous in all cases to want to simply remove all "old" folders with a simple syntax.
I find it healthier (it's quite easy via FORFILES or ROBOCOPY) to list not the folders but all the files prior to this date and delete them, then check if the remaining folders are empty.
There are several more or less convoluted syntaxes, the simplest one consists of moving the folder onto itself via ROBOCOPY:
ROBOCOPY "Myfolder" "Myfolder" /S /MOVE