I want to remove directories of the following format:

C:\Program Files\FogBugz\Plugins\cache\PluginName@example.com_NN

NN is a number, so I want to use a wildcard (this is part of a post-build step in Visual Studio). The problem is that I need to combine quotes around the path name (for the space in Program Files) with a wildcard to match the end of the path. I already found out that rd is the remove command that accepts wildcards, but where do I put the quotes? I have tried no ending quote (works for dir), ...example.com*", ...example.com"*, ...example.com_??", ...cache\"PluginName@example.com*, ...cache"\PluginName@example.com*, but none of them work.

(How many commands to remove a file/directory are there in Windows anyway? And why do they all differ in capabilities?)


rmdir does not support wildcards. It only accepts complete filenames.

You can try this alternative:

for /d %f in ("C:\Program Files\FogBugz\Plugins\cache\PluginName@example.com_*") do rmdir /s/q "%~f"

(The /s/q arguments to rmdir do the same thing as rm -rf on Unix. The for /d argument makes for match directory names instead of file names.)

Remember that the cmd.exe shell does not do wildcard expansion (unlike Unix sh) -- this is handled by the command itself.

  • Your alternative works in cmd.exe, but not as a VS post-build step (The command exited with code 255). And indeed, from the (conflicting) info om the web, I thought that rd did support wildcards, and rmdir didn't. But it appears they are synonyms? And indeed, I was expecting expansion by the shell, not the individual commands. – Jan Fabry Jun 2 '10 at 14:48
  • 1
    @Jan: That might be because for is a cmd.exe built-in. You could try putting it in a .cmd script and run that instead. (Beware: for inside batch scripts requires %%x instead of %x.) And yes, rd and rmdir are the same thing. – user1686 Jun 2 '10 at 15:02

You can escape the space with the ^ character: C:\Program^ Files\FogBugz\Plugins\cache\PluginName@example.com_*

  • That doesn't seem to work (although it should: msdn.microsoft.com/en-us/library/kcc7tke7(VS.80).aspx ). I use the following code: rmdir /s /q C:\Program^ Files\FogBugz\Plugins\cache\$(TargetName)* in VS or in cmd.exe (with TargetName replaced of course), and I get The system cannot find the file specified. / The system cannot find the path specified. in both places. – Jan Fabry Jun 2 '10 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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