Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying Files with Wildcards in the Path

Tags:

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

like image 427
Skcussm Avatar asked Dec 22 '11 19:12

Skcussm


People also ask

What is wildcard file path?

Rather than entering each file by name, using wildcards in the Source path allows you to collect all files of a certain type within one or more directories, or many files from many directories.

Which wildcard is used for copying all files?

As an alternative to entering a new name for each file, you can use a simple wildcard system to rename all copied or moved files at once. This is not a full pattern matching (or regular expressions) system; instead, the only wildcard character that's recognised is * (the asterisk).

Can you use wildcards in Xcopy?

[source] – Pathname for the file(s) to copy (accepts wildcards). You should specify the drive, path, and the files you would want to copy. [destination] – Pathname for the new file(s). If you don't specify the destination path, the command will copy the files to the same path as the source.

How do you copy a path with spaces?

Use quotation marks when specifying long filenames or paths with spaces. For example, typing the copy c:\my file name d:\my new file name command at the command prompt results in the following error message: The system cannot find the file specified. The quotation marks must be used.


3 Answers

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.

like image 94
dbenham Avatar answered Oct 17 '22 19:10

dbenham


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
like image 7
Rus Future Avatar answered Oct 17 '22 17:10

Rus Future


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.

like image 6
PA. Avatar answered Oct 17 '22 18:10

PA.