tags:

views:

219

answers:

1

Hi all,

Using CMD line, in a given directory, i want to detect the most recently created/written folder and delete all the contents of that folder... Any help/suggestions would be helpful... Thanq in advance..

Regards, chandra

A: 

This command prints all subdirectories in order of their last write/created time in reverse order (latest directories first):

DIR /A:D /O:-D /TW /B

To delete a directories' contents, a simple

DEL /S /Q "directory"

should be sufficient

If you want to process only the first result of the DIR command, you can use a FOR loop in a batch file, that leaves after the first iteration. It should look something like this:

@ECHO OFF

REM delete all contents from the sub directory most recently created or written to
FOR /F "delims=" %%A IN ('DIR /A:D /O:-D /TW /B') DO (
   RD /S /Q %%A
   EXIT /B
)

Only works for the subdirectories of the current working directory, so use with care! I guess for empty directories there will be some weird output, but I didn't test it.

EDIT:

Updated the batch file to remove the whole directory and its content using:

RD /S /Q "directory"
Frank Bollack
As you guessed, it is not working for empty directories. For non-empty directories, the code you have suggested is able to delete only the contents of the most recently created/edited folder....but, it is not the folder too. I wish to delete even the folder along with its contents. Thanks for your help Frank..
chandra
See my updated answer.
Frank Bollack