1.

Solve : Batch Script to Delete The Oldest Folder With a Specifc Name?

Answer»

I spent a little time on this one a can't seem to find exactly what I need. I don't have anything to OFFER because I'm truly stumped. I have 3 folders named like this:

Rox
Rox07122011
Rox09302012

These folders reside in a directory with many other folders. Via a batch script I want to be able to delete the "Rox" folder (and only the "Rox" folder) in this directory with the oldest Modified date but I don't know how to do it.

Please tell me how to do this.

Thank you,

MJ(untested) This will set folder to each rox* directory name in sequence from newest to oldest, and remember the oldest. Then the RD command is echoed to the screen

If it's right then remove the echo keyword.


Code: [Select]@echo off
for /f "delims=" %%a in ('dir "rox*" /a:d /o:-d /B') do set "folder=%%a"
echo rd /s /q "%folder%"
pauseFoxidrive.
/O:D is oldest FIRST. (So the newest will be the LAST out of the for loop)
/O-D is the newest first. (So the oldest will be the last out of the for loop)It would be great if people will realize that naming their folders with a date it is always best to use YYYYMMDD.
Not sure if we can guarantee that the date in folder name is actually the same as the modified date in the file attributes.
We could manipulate the folder names to a temp file and then sort it.Quote from: Squashman on October 12, 2012, 06:21:03 AM

Foxidrive.
/O:D is oldest first. (So the newest will be the last out of the for loop)
/O-D is the newest first. (So the oldest will be the last out of the for loop)

Thanks, I modified it to -d

It's not creation date though, and that could be what the OP wants.
This is the same thing but sorted by creation date.

Code: [Select]@echo off
for /f "delims=" %%a in ('dir "rox*" /t:c /a:d /o:-d /b') do set "folder=%%a"
echo rd /s /q "%folder%"
pause


And yes, foldernames with ROX-YYYYMMDD are certainly preferable to sort by.Thank you! Works like a charm. I appreciate it. It's inspired me to take it a step further. How can I run the loop against a text file that contains a list of servers?

Text file will include computer names:

Computer1
Computer2
Computer3
etc. . . .

Do I SIMPLY place the path to the text file in the parenthesis ("" ' dir "Rox*" . . . )?

Thanks again,

MJPerhaps I can nest the for loops like this:

Code: [Select]For /F "Tokens=*" %%I IN (textfile.txt) DO (
for /f "delims=" %%a in ('dir "\\<path-to-\Rox*" /t:c /a:d /o:-d /b') do set "folder=%%a"
rd /s /q "%folder%"

Will that work?


Discussion

No Comment Found