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
For those using Altap Salamander (2 panels file manager) : in the Options of the Copy popup, just specify the file names or masks. Easy.
If Powershell is an option, you can do this:
The main disadvantage is it copies all folders, even if they will end up being empty because no files match the filter you specify. So you could end up with a tree full of empty folders, in addition to the few folders that have the files you want.
I am fine with regular expressions, lazy and averse to installs, so I created a batch file that creates the directory and copies with vanilla DOS commands. Seems laborious but quicker for me than working out robocopy.
(.*)
replace string"\1"
, and click replace all(.*)
replace stringmd qqq\1\nxcopy \1 qqq\1\n
and click replace all\\([^\\^"]+)"\n
replace\\"\n
A:\src
andB:\dest
). Turn OFF regular expressions, searchqqq"A:\src\
replaceB:\dest\
and click replace all.md will create nested directories. copy would probably behave identically to xcopy in this example. You might want to add /Y to xcopy to suppress overwrite confirms. You end up with a batch file like so:
repeated for every file in your original list. Tested on Win7.
You don't mention if it has to be batch only, but if you can use
ROBOCOPY
, try this:EDIT: Changed the
/S
parameter to/E
to include empty folders.With find and cp only:
Under Linux and other UNIX systems, using the
tar
command would do this easily.Then you'd cwd to the target and:
Of course you could pipe the output from the first tar into the 2nd, but seeing it work in steps is easier to understand and explain. I'm missing the necessary cd /to/new/path/ in the following command - I just don't recall how to do it now. Someone else can add it, hopefully.
Tar (gnutar) is available on Windows too, but I'd probably use the
xcopy
method myself on that platform.