Skcussm
Skcussm

Reputation: 698

Copying Files with Wildcards in the Path

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".

Upvotes: 25

Views: 96679

Answers (5)

SDey
SDey

Reputation: 1

So if you know the directory name I'm not sure why you are attempting to use wildcards. I had a problem where we use Mozilla Firefox and many websites we use require Common Access Card Authentication. By default Mozilla Firefox isn't configured to utilize a CAC Reader. So I configured my Firefox with the CAC Reader and then grabbed the cert8.db and secmod.db files from my User Directory C:\Users\%myname%\AppData\Roaming\Mozilla\Firefox\Profiles\???????.default

The problem was copying those files to multiple computers and multiple user directories when I don't know who is logged onto what computer and the ???????.default folder is named something different for every user.

I created a text file called Computers.txt and input the list of our computers I retrieved from Active Directory and then I wrote the following script to copy the .db files that have the CAC Device which I copied to C:\Temp\Security

for /f %c in (C:\Temp\Computers.txt) do for /f %u in ('dir /B \\%c\C$\Users\') do for /f %m in ('dir /B \\%c\C$\Users\%u\AppData\Roaming\Mozilla\Firefox\Profiles\') do copy /Y C:\Temp\Security\*.db \\%c\C$\Users\%u\AppData\Roaming\Mozilla\Firefox\Profiles\%m\*

Upvotes: 0

Colin Hay
Colin Hay

Reputation: 11

This is my batch script for backing up some folders to one drive and some to another using the above help - works on Win 7 great.

There is no need to copy the folder structure if you use a FOR loop and /S will just copy whats changed

Just a shame you can't use regular expressions here - or does someone have a neater solution ?

FOR /d %%a in (2010* 2011* 2012* 201301* 2013020* 2013021* 20130222* 20130224*) DO (
  XCOPY "%%a" "Z:\Backup\My Videos\%%a\" /D /S /Y | findstr /V "0 File"
)
FOR /d %%a in (20130225* 201303*) DO (
  XCOPY "%%a" "X:\Backup\My Videos\%%a\" /D /S /Y | findstr /V "0 File"
)

Upvotes: 1

Rus Future
Rus Future

Reputation: 71

RusF. Note:

Using XCOPY, it is no longer possible to use wildcarding in the file name for directories, and then just have a group of similarly named directories copied over to an archive or network structure.

(Hint: You want to do this all the time, if you are managing big wads of data. Eg. Video or movie files (where many files may be associated with the production. Or, price history data files, in various formats, or database files from various research projects, or experimental work.)

I am sure I used to be able to do this with older XCOPY, but with upgrades (for security), wildcards in the file name, do not work right (ie. you can build a directory structure, with the "/t" option, but you cannot get the damn files within the directories to migrate! Damned annoying.)

Well, we here at the Farm, always get to a solution. Even if it takes using a .44 magnum or a Styer 50. :)

So, heres what I came up with:

Assume you have a bunch of directories called "Fark_1, Fark_2, Fark-Special, Fark_in_the_Park, Fark99... " and so on.

If all these "Fark..." directories exist on a C:\ disk, and you want to migrate a copy of this sub-structure to the archive disk on the network (say it is called: "h:") you can just do the following:

Run a FOR DO from Windows (MS-DOS) command prompt:

 c:\>  XCOPY  fark*  h: /s /e /f /t

            The /s /e are for copying subdirs
            and empty directories, the /f says to
            show the files, and the /t says to only
            copy the disk directories.  No XCOPY
            option seems to exist anymore to copy the
            disk structure AND the files in the
            directories.

To get the files to copy over, you then do the following, also from Windows command prompt:

 FOR /d %a in (fark*) DO XCOPY "%a" h:%a\ /s /e /f /h /k /y

            The /d says we are using directory names.
            The %a is the variable name, substituted
            into the command script for XCOPY. Make
            sure to use whatever logical name or drive
            letter you have for your archive structure.
            (ie. don't use h: in the above command,
            unless you have mapped h: to the place on
            the network where you want to copy your
            files to.)  And note, the /y option means
            overwrite any files already there.

Yes, this is old-school unix/dos stuff, but it works, and you can put it into a batch job. And you can avoid being the mouseboy to get the work done. (Point, click, grunt, curse. Repeat. The canonical process for using a GUI, right?) Now, there are lots of fancy ways to do this. And various utils like Robocopy, XXcopy and so on. But this gets it done, and if you have any copy of Windows, you probably already have the XCOPY and FOR-DO thingys. All the best. Oh, if you slot the two commands into a batch job, then you have to adjust the syntax for two levels of command substitution (I think is the correct term).

batch example:

 REM --- copy the "fark*" files to the archive disk h:
 @echo off
 REM --- create/update the structure 
 xcopy fark* h: /s /e /f /t
 REM --- copy over the fark* dirs, and all associated subdirs
 for /d %%a in (fark*) do xcopy "%%a" "h:%%a\" /s /e /f /h /k /y
  • Rus Future April 18, 2012

Upvotes: 7

PA.
PA.

Reputation: 29339

in case you want to copy all the files for all the directories that match. you may still do something like this...

for /d %%d in (bo*) do (
  for /d %%e in (ma*) do (
     copy %%d\%%e\* c:\temp

but be careful for the case when files with the same name appera in different directories.

Upvotes: 6

dbenham
dbenham

Reputation: 130809

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.

Upvotes: 25

Related Questions