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
Using WinRAR command line interface, you can copy the file names and/or file types to an archive. Then you can extract that archive to whatever location you like. This preserves the original file structure.
I needed to add missing album picture files to my mobile phone without having to recopy the music itself. Fortunately the directory structure was the same on my computer and mobile!
I used:
This created an archive with all the Folder.jpg files in the proper subdirectories.
This technique can be used to copy file types as well. If the files all had different names, you could choose to extract all files to a single directory. Additional command line parameters can archive multiple file types.
More information in this very helpful link http://cects.com/using-the-winrar-command-line-tools-in-windows/
Similar to Paulius' solution, but the files you don't care about are not copied then deleted:
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.
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).
Thanks To Previous Answers. :)
This script named "r4k4copy.cmd":
It accepts variable of "Source", "Destination", and "FileName". It also can only copying specified type of files or selective filenames.
Any improvement are welcome. :)
To copy all text files to G: and preserve directory structure:
An alternate solution that copies one file at a time and does not require ROBOCOPY:
The outer for statement generates any possible path combination of subdirectory in
SOURCE_DIR
and name inFILENAMES_TO_COPY
. For each existing file xcopy is invoked.FILE_INTERMEDIATE_DIR
holds the file's subdirectory path withinSOURCE_DIR
which needs to be created inDEST_DIR
.