Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete windows folder using pattern matching

I want to delete folders within a folder by identifying the folders with specific patter that I'm looking for. ..for example if there are 6 folder as below, i want to delete folders with pattern that has string "-dated" as part of folder name ... can some one help me how I can do this in a batch file

1 . "target-dated-29sep"
2 . "target-dated-28sep"
3 . "target"
4 . "target-dated-27sep"*
5 . "BIN"
like image 862
keylan Avatar asked Apr 10 '26 09:04

keylan


1 Answers

You can use the FOR /D %variable IN (set) DO command [command-parameters] with a pattern in the set part of the command like so:

FOR /d %%a in (\*dated\*) DO RD /s /q "%%a"

NOTES:

  • The % needs to be escaped inside the batch file, whereas directly in the command line you would only need a single % sign.
  • This assumes you are running the batch script directly from the same path as the folders.
  • For more information check the command line help: for /? and rd /?
like image 130
Jason Down Avatar answered Apr 12 '26 15:04

Jason Down