views:

345

answers:

2

How would you write this having only batch files and VBScript at your disposal in a Windows environment?

find -name '*.ext' -exec cp --parents {} destination/ \;

Or put it this way:

  • Look recursively in the current folder for some filename pattern (ending with an extension called ext in the example above),
  • Copy each of these files to a destination folder (wherever that is) preserving directory tree structure (or creating any missing intermediate directories) as in current folder.
A: 

Edit: My bad, I missed the --parents flag in cp command... xcopy as shown below will on only recreated the downward path, not that of the directories "above" current directory.

I [now] believe xcopy would [not] do the trick [alone. But works wonders with Grant Wagner's "for /r" trick].

xcopy [some_path]*.ext [some_otherpath]\final_directory /E  

 (or /S, above, if you don't want empty directories)
 the elements within brackets, some_path and some_otherpath are optional
mjv
How precisely please? I've tried with /t and /i options, but that didn't cut.
exalted
`/T` only creates the directory structure but doesn't copy anything, while `/I` simply alters the semantics for the target. I suspect you would want the `/S` flag. So something along the lines of `xcopy *.ext destination /S`
Joey
/s would work from the top to the bottom, wouldn't it? I need to copy only files with the given pattern and their directory tree structure. That's what the bash command does, exactly.To better understand this, imagine having thousands of various kinds of file in the current folder, but you only need those specific ones.
exalted
+2  A: 

This should work:

for /r %%a in (*.cmd) do xcopy %%a C:\DESTINATION%%~pa

Note that DESTINATION should never be a subdirectory of the directory you are trying to copy from, otherwise for /r goes into a recursive loop copying files it has already copied creating longer and longer directory paths (don't ask me how I know).

You could make it slightly more robust using xcopy /c (to continue copying even if errors occur). You may also want to look at xcopy /? to see if there is anything else of value there (/q, /r, /o, etc).

Grant Wagner
It will eventually stop once the paths reach 32k characters :-)
Joey
+1 Works like a charm, Grant! Only idiots would put a circular reference in the destination, right? LOL.
mjv
It works quite nice (almost there), thanks Grant! But the directory tree structure created in destination is actually absolute path of the file(s). Is there any way to create only relative path for this?
exalted
@exalted: If what you need is the relative directories from your current directory to a new location, preserving the paths, then [mjv's answer][http://stackoverflow.com/questions/1439588/1439636#1439636] `xcopy /s *.cmd C:\DESTINATION` should work as long as you run it in the source directory. If you run it from another directory `xcopy /s [source_dir]\*.cmd C:\DESTINATION` you'll get `C:\DESTINATION\[source_dir]\directories_and_files`.
Grant Wagner