Copying Files with Wildcards in the Path

batch-file

Assume there exists the directory structure C:\users\myuser\Desktop\bob\marley.

In windows, from the starting directory C:\users\myuser\Desktop I can do "cd bo*\mar*" and cd in to the directory bob\marley. However, I can't do a "copy bo*\mar* C:\"

I need to use the copy, xcopy, or some standard windows command with wildcards in the path to copy the match directories to the destination. I am not sure if this is a limitation of copy and xcopy or if I have the syntax wrong. I've also tried "xcopy /E /I bo*\mar** C:\somedirectory".

Best Answer

No - you cannot do that with COPY or XCOPY. There is a fundamental flaw with what you are asking.

What if you also have a directory named "boston\marathon"? Which directory do you want to copy from?

You might answer - "I want both". But now you have a nasty potential problem. What if both source directories contain a file named "test.txt". You can only have one file named "test.txt" in your destination. Which one do you keep? There is no correct answer, and that is a good reason why the command is designed to disallow what you are attempting to do.

Addendum

If you are willing to assume that only one folder matches the pattern, then you already know all the steps to accomplish your goal with the following.

@echo off
cd bo*\ma*
xcopy * c:\somedirectory

If you want to return to where you started, then use PUSHD/POPD instead of CD

@echo off
pushd bo*\ma*
xcopy * c:\somedirectory
popd

BUT, you should be very careful. Boston\marathon could exist, and neither PUSHD nor CD will give any indication that there is a potential problem.

Related Topic