How to copy a directory structure but only include certain files (using windows batch files)
Solution 1
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.
Solution 2
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
.
Solution 3
try piping output of find (ie. the file path) into cpio
find . -type f -name '*.jpg' | cpio -p -d -v targetdir/
cpio checks timestamp on target files -- so its safe and fast.
remove -v for faster op, once you get used to it.
Solution 4
If Powershell is an option, you can do this:
Copy-Item c:\sourcePath d:\destinationPath -filter data.zip -recurse
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.
Solution 5
To copy all text files to G: and preserve directory structure:
xcopy *.txt /s G:
Related videos on Youtube
Comments
-
M4N almost 2 years
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
-
Paulius over 15 yearsit doesn't preserve the folder structure.
-
M4N over 15 yearsThanks 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 over 12 yearsThanks, but I was looking for a solution to be used from script (batch) files. So, no drag&drop...
-
gkrogers almost 12 yearsThe /E option preserves the folder structure, including creating empty folders if necessary. The filename also works with a wildcard (e.g. folder1\*.zip). I only get the "File not found error" when I pass it a full filename that doesn't exist (in which case I get "File not found - xxx.xxx 0 File(s) copied". When I give it a pattern that returns no files (e.g. folder1\*.zzz), I just get "0 File(*s) copied".
-
Niels Brinch over 11 yearsCan I copy *.zip with this syntax? I tried just writing *.zip instead of data.zip, but it didn't copy the files, only the folders. I also tried ?.zip
-
jonsinfinity over 11 years@Niels Brinch Yes, you should be able to do that. What is the exact command line you are using?
-
Niels Brinch over 11 yearsROBOCOPY C:\Source C:\Destination *.zip /E
-
jonsinfinity over 11 yearsWeird. What OS are you using? Do you have the necessary file/folder permissions? I just tested on Win 7 and
*.zip
worked for me. -
Niels Brinch over 11 yearsReally!? I am also using Win 7. When I try, it just doesn't copy any files. Only folders. I'll try again. (while we're at it, what is the syntax to include more than *.zip - let's say *.zip and *.jpg.
-
jonsinfinity over 11 yearsI only tested
ROBOCOPY C:\Source C:\Destination *.zip /E
, but you should be able to include.jpg
, like thisROBOCOPY C:\Source C:\Destination *.zip *.jpg /E
. -
ssinganamalla almost 11 yearstar cvf /tmp/full-structure.tar **/data.zip **/info.txt worked for me
-
Crashworks about 10 years@Christian it has a dependency on cpio which isn't part of standard install.
-
user4317867 about 9 yearsLate to the party comment, but for these types of requests it might help to store the external command (in this question RoboCopy.EXE) on a server. Then within the bat file,
pushd \\server\share
and execute your RoboCopy commands, then usepopd
to return to the local system. Now you can run commands external to a server without installing them. -
Avatar about 8 yearsI needed to copy all photos from folders and subfolders, this batch script helped. Just changed the line
set FILENAMES_TO_COPY=data.zip info.txt
above toset FILENAMES_TO_COPY=*.jpg
-
Nike over 7 yearsBoth "tar cvf /tmp/full-structure.tar **/data.zip **/info.txt" and "tar cvf /tmp/full-structure.tar *data.zip *info.txt" gave me 3 errors: (1) "tar: *data.zip: Cannot stat: No such file or directory" , (2) "tar *info.txt: Cannot stat: No such file or directory" , (3) "tar: Exiting with failure status due to previous errors". From whcih folder do I do all this? I do not understand this method.
-
Nike over 7 yearsDoes anyone know how to do this in Linux? I searched this whole page and found one answer with a suggestion for how to do it in Linux, but it gave 3 errors, which I have shown in the comment to that answer.
-
Anfelipe almost 6 yearsThe question says WINDOWS
-
JohnP almost 6 yearsWin10 has WSL,which makes this valid. tar has been available on Windows for 20+ yrs and the use of a pipe has worked since the MS-DOS days. In any shell environment, the PWD is important. PWD - present working directory. I will admit this solution is less for end users and more for power-users or programmers.
-
ricardo.scholz about 5 yearsI'm getting an error because the file path has spaces on it (SOURCE_DIR="C:\Origin Test", DEST_DIR="C:\Dest Test", error: Test"" was unexpected at this moment). I've tried to change %var% by !var! everywhere, but without success. Any clues on how to solve this? The whole structure is full of folder names with spaces.
-
sakra about 5 yearsTry
set "SOURCE_DIR=C:\Origin Test"
andset "DEST_DIR=C:\Dest Test"
. Note the placement oft the double quotes. -
MakotoE almost 5 yearsRunning this a second time will change the destination. (It puts the files in a subfolder.) It's a silly behavior if you ask me. You will want to run
rm -r -ErrorAction:SilentlyContinue d:\destinationPath
before copying to fix that. -
Micka over 4 yearscan this work recursively, like copying all .txt from subfolders and preserving directory structure relative to the base directory?
-
Anurag S Sharma almost 3 years@Micka yes it does
-
Anton Duzenko over 2 yearsHow would I also delete the copied files in source dir?
-
jonsinfinity over 2 years@AntonDuzenko Maybe the
MOV
orMOVE
parameters for ROBOCOPY will help you. RunROBOCOPY /?
from a command line to see more info. Setup some test folders and files before using on anything important.