keylan
keylan

Reputation: 33

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"

Upvotes: 3

Views: 3484

Answers (1)

Jason Down
Jason Down

Reputation: 22211

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 /?

Upvotes: 6

Related Questions