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?

Answers


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.


Need Your Help

What is required when overriding CreateObjRef()? Do I even have to?

c# remoting filestream

I am writing a descendant of FileStream which deals transparently with files with meta-data bytes (raw reads from flash chips for example). It automatically skips over the metadata bytes so that the

iOS: Replacing modal ViewController with UIModalTransitionStyleFlipHorizontal

iphone ios uiview uiviewcontroller

I present a modal ViewController using presentViewController. Now I want to flip this modal view using UIModalTransitionStyleFlipHorizontal so from within the presented ViewController I call:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.