LCaraway
LCaraway

Reputation: 1357

Command Prompt - Remove Directories With Specific Name Patern

I need us a bat file that will delete specific directories as long as they meat two specific critrera. 1) If the directory is older than 14 days 2) if the directory name consists of all numbers.

I have the following code that addresses only criteria 1, that is, the directory is older than 14 days. How would I extend this to then only delete those with numeric based names. FORFILES /D -14 /C "cmd /c IF @isdir == TRUE rmdir @path /s /q"

Upvotes: 0

Views: 243

Answers (1)

dbenham
dbenham

Reputation: 130919

Since this will be run as a scheduled job, you should probably specify the root folder.

You can use FINDSTR to verify the name consists solely of digits. The leading and trailing dots are needed to match the quotes that are included in @file.

pushd "yourRootPath"
FORFILES /D -14 /C "cmd /c IF @isdir == TRUE echo @file|>nul findstr /x .[0-9]*.&&rd /s /q @path"

However, the above may be quite slow. It would probably be faster to iterate the numeric folder names and then call FORFILES for each candidate folder to test the date:

pushd "yourRootPath"
for /f "delims=" %%F in ('"dir /b /ad|findstr /x [0-9]*"') do 2>nul forfiles /d -14 /m %%F /c "cmd /c rd /s /q @path"

Upvotes: 2

Related Questions