Help: Wildcards

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ThomasLMahoney
Posts: 1
Joined: 01 Feb 2012 09:11

Help: Wildcards

#1 Post by ThomasLMahoney » 03 Feb 2012 09:24

How do I use wildcard characters in commands? The closest I've gotten is:

Code: Select all

xcopy C:\"My Documents"\*.doc* D:\Documents\College\


Where *.doc* is any Word file that has the extension .docm or .docx. But it doesn't work. Can someone help me? :?

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: Help: Wildcards

#2 Post by Squashman » 03 Feb 2012 10:20

So you are basically saying you want *.DOCM and *.DOCX but not *.DOC ?

Right now your code would copy all three of them.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help: Wildcards

#3 Post by aGerman » 03 Feb 2012 11:56

I can't find any fault except what Squashman already mentioned and that you should enclose the entire path in quotes instead of only the folder name with spaces.

Code: Select all

xcopy "C:\My Documents\*.doc*" "D:\Documents\College\"

But "it doesn't work" isn't an adequate description of what happened. Did you get an error message? Was nothing copied at all? Were the wrong files copied? ...

Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Help: Wildcards

#4 Post by alan_b » 04 Feb 2012 02:38

aGerman wrote:you should enclose the entire path in quotes instead of only the folder name with spaces.

Please teach me some more - I thought I only needed to enclose spaces.
Is the problem that "*.doc" could expand to
"My file name has spaces also.doc"

Regards
Alan

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help: Wildcards

#5 Post by foxidrive » 10 Feb 2012 07:10

Microsoft parsing can accept the type of quoting you used originally but I doubt it will work in all cases, whereas when the entire path is quoted then it is guaranteed to work.

This is untested:

Code: Select all

@echo off
for %%a in (*.docm *.docx) do  xcopy "C:\My Documents\%%a" "D:\Documents\College\"



With respect to the term *.doc
when cmd searches for files it matches the short filename as well as the long filename, and in a long filename the first three characters of an extension are used for the short filename - so "my file.docx" might use a short filename of myfile~1.doc and so is matched when searching for *.doc and also when searching for *.doc*

Post Reply