简体   繁体   中英

Recursively copy file-types from directory tree

I'm trying to find a way to copy all *.exe files (and more, *.dtd, *.obj, etc.) from a directory structure to another path.

For example I might have:

Code
   \classdirA
             \bin
                 \classA.exe
   \classdirB
             \bin
                 \classB.exe
   \classdirC
             \bin
                 \classC.exe
   \classdirD
             \bin
                 \classD.exe

And I want to copy all *.exe files into a single directory, say c:\\bins What would be the best way to do this?

Constraints for my system are:

  • Windows
  • Can be Perl, Ruby, or .cmd

Anyone know what I should be looking at here?

Just do in Ruby, using method Dir::glob :

# this will give you all the ".exe" files recursively from the directory "Code".
Dir.glob("c:/Code/**/*.exe") 

** - Match all directories recursively. This is used to descend into the directory tree and find all files in sub-directories of the current directory, rather than just files in the current directory. This wildcard is explored in the example code.

* - Match zero or more characters. A glob consisting of only the asterisk and no other characters or wildcards will match all files in the current directory. The asterisk is usually combined with a file extension, if not more characters to narrow down the search.

Nice blog Using Glob with Directories .

Now to copy the files to your required directory, you need to look into the method, FileUtils.cp_r :

require 'fileutils'
FileUtils.cp_r Dir.glob("c:/Code/**/*.exe"), "c:\\bins"

I just have tested, that FileUtils.cp method will also work, in this case :

require 'fileutils'
FileUtils.cp Dir.glob("c:/Code/**/*.exe"), "c:\\bins"

My preference here is to use ::cp method. Because Dir::glob is actually collecting all the files having .exe extensions recursively, and return them as an array. Now cp method is enough here, now just taking each file from the array and coping it to the target file.

Why I am not liking in such a situation, the method ::cp_r ?

Okay, let me explain it here also. As the method name suggests, it will copy all the files recursively from the source to target directory. If there is a need to copy specific files recursively, then ::cp_r wouldn't be able to do this by its own power ( as it can't do selections by itself, which ::glob can do ). Thus in such a situation, you have to give it the specific file lists, it would then copy then to the target directory. If this is the only task, I have to do, then I think we should go with ::cp , rather than ::cp_r .

Hope my explanation helps.

From cmd command line

for /r "c:\code" %f in (*.exe) do copy "%~ff" "c:\bins"

For usage inside a batch file, double the percent signs ( %% instead of % )

Windows shell ( cmd ) command:

for /r code %q in (*.exe) do copy "%q" c:\bin

Double the % characters if you place this in a batch file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM