How to combine wildcards and spaces (quotes) in an Windows command?

2

2

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

Jan Fabry

Posted 2010-06-02T10:14:07.643

Reputation: 157

I usually use rm -rf for this. There are several ports of Unix coreutils to Windows: UnxUtils, the MSYS package of MinGW, Cygwin (yecch), Micros~1 own Services for Unix...

– user1686 – 2010-06-02T12:42:21.543

Answers

3

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.

user1686

Posted 2010-06-02T10:14:07.643

Reputation: 283 655

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 – 2010-06-02T14:48:33.577

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 – 2010-06-02T15:02:56.123

0

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

Darth Android

Posted 2010-06-02T10:14:07.643

Reputation: 35 133

That doesn't seem to work (although it should: http://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 – 2010-06-02T11:52:45.050