I want to delete the oldest and second eldest folder from a drive using a Windows Command Line script.

This is running from BackupAssist, which only supports windows command line commands/DOS in the GUI for pre or post-backup scripts. I'd rather not write a command line to call into a Powershell script.

Thanks

link|improve this question

30% accept rate
feedback

1 Answer

up vote 2 down vote accepted

This will find the oldest and next oldest directories in the current directory. Comment out the echo commands and uncomment the rd commands to make it functional.

@echo off
rem cmd file to delete oldest and second oldest directories
setlocal enabledelayedexpansion
for /F "delims=" %%i in ('dir /b/ad-l/o-d') do @set nextoldest=!oldest! & SET oldest=%%i
echo %oldest%
echo %nextoldest%
rem rd /s/q %oldest%
rem rd /s/q %nextoldest%

If you want to make it look at the whole drive, change the dir command to look like this:

dir /b/ad-l/o-d/s \

That will take a long time.

Note that I excluded junction points with -l.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.