views:

5697

answers:

7

As the title says, how can I recursively copy a directory structure but only include some files. E.g given the following directory structure:

folder1
  folder2
    folder3
      data.zip
      info.txt
      abc.xyz
    folder4
    folder5
      data.zip
      somefile.exe
      someotherfile.dll

The files data.zip and info.txt can appear everywhere in the directory structure. How can I copy the full directory structure, but only include files named data.zip and info.txt (all other files should be ignored)?

The resulting directory structure should look like this:

copy_of_folder1
  folder2
    folder3
      data.zip
      info.txt
    folder4
    folder5
      data.zip
+4  A: 

You don't mention if it has to be batch only, but if you can use ROBOCOPY, try this:

ROBOCOPY C:\Source C:\Destination data.zip info.txt /E

EDIT: Changed the /S parameter to /E to include empty folders.

aphoria
Robocopy is fine. A solution without it would be even better.
M4N
I think you might want /E instead if /S in this case.
cmsjr
@cmsjr: yes /E is required in my case
M4N
Oops...You're right should be /E.
aphoria
Wow, a down vote?
aphoria
This should not have been downvoted, since it is a correct solution.
M4N
I'm accepting this answer, because it is the most elegant (and efficient?). Thanks to eveyone.
M4N
Cool, thanks Martin.
aphoria
A: 

If a windows desktop app can help, here it is: PikyBasket - http://www.conceptworld.com/Piky/

goths
It should be a command line solution. And in addition I can't use a commercial product.
M4N
The new version of Piky Basket called Copywhiz does have command line options.
goths
+1  A: 
XCOPY /S folder1\data.zip copy_of_folder1  
XCOPY /S folder1\info.txt copy_of_folder1

EDIT: If you want to preserve the empty folders (which, on rereading your post, you seem to) use /E instead of /S.

gkrogers
This give an error: "File not found - data.zip".
M4N
it doesn't preserve the folder structure.
Paulius Maruška
+2  A: 

That's only two simple commands, but I wouldn't recommend this, unless the files that you DON'T need to copy are small. That's because this will copy ALL files and then remove the files that are not needed in the copy.

xcopy /E /I folder1 copy_of_folder1
for /F "tokens=1 delims=" %i in ('dir /B /S /A:-D copy_of_files ^| find /V "info.txt" ^| find /V "data.zip"') do del /Q "%i"

Sure, the second command is kind of long, but it works!

Also, this approach doesn't require you to download and install any third party tools (Windows 2000+ BATCH has enough commands for this).

Paulius Maruška
Thanks a lot. As you mentioned, it is not ideal, because (in my case) the files that must be copied are usually small, while the files that should not be copied are larger (and there may be lots of them).
M4N
+2  A: 

Similar to Paulius' solution, but the files you don't care about are not copied then deleted:

@echo OFF

:: Replace c:\temp with the directory where folder1 resides.
cd c:\temp

:: You can make this more generic by passing in args for the source and destination folders.
for /f "usebackq" %%I in (`dir /b /s /a:-d folder1`) do @echo %%~nxI | find /V "data.zip" | find /v "info.txt" >> exclude_list.txt
xcopy folder1 copy_of_folder1 /EXCLUDE:exclude_list.txt /E /I
Patrick Cuff
nice. got my up-vote.
Paulius Maruška
Thanks :) That's mighty kind of you.
Patrick Cuff
A: 

I have a similiar situation. What if I wanted a batch to copy the entire folder structure of a directory and move all files in those folders that were modified before 1/1/2008? Is this possible?

You should probably post your own question to get an answer.
M4N
+1  A: 

An alternate solution that copies one file at a time and does not require ROBOCOPY:

@echo off
setlocal enabledelayedexpansion

set SOURCE_DIR=C:\Source
set DEST_DIR=C:\Destination
set FILENAMES_TO_COPY=data.zip info.txt

for /R "%SOURCE_DIR%" %%F IN (%FILENAMES_TO_COPY%) do (
    if exist "%%F" (
     set FILE_DIR=%%~dpF
     set FILE_INTERMEDIATE_DIR=!FILE_DIR:%SOURCE_DIR%=!
     xcopy /E /I /Y "%%F" "%DEST_DIR%!FILE_INTERMEDIATE_DIR!"
    )
)

The outer for statement generates any possible path combination of subdirectory in SOURCE_DIR and name in FILENAMES_TO_COPY. For each existing file xcopy is invoked. FILE_INTERMEDIATE_DIR holds the file's subdirectory path within SOURCE_DIR which needs to be created in DEST_DIR.

sakra