Delete Windows folder using pattern matching

I want to delete folders in a folder by specifying folders with the specific patter I am looking for. For example, if there are 6 folders as below, I want to delete folders with a template that has the string "-dated" as part of the folder name ... can anyone help me how can I do this in a batch file

1 . "target-dated-29sep"
2 . "target-dated-28sep"
3 . "target"
4 . "target-dated-27sep"*
5 . "BIN"



source to share

1 answer

You can use FOR /D %variable IN (set) DO command [command-parameters]

with a template in the given part of the command, for example:

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



  • %

    must be escaped inside a batch file, whereas directly on the command line you only need one character %

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

    andrd /?



All Articles