How to copy a directory structure but only include certain files (using windows batch files)

223,978

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:
Share:
223,978

Related videos on Youtube

M4N
Author by

M4N

Software developer, mainly using ASP.NET/C#. #SOreadytohelp

Updated on July 08, 2022

Comments

  • M4N
    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
    Paulius over 15 years
    it doesn't preserve the folder structure.
  • M4N
    M4N over 15 years
    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
    M4N over 12 years
    Thanks, but I was looking for a solution to be used from script (batch) files. So, no drag&drop...
  • gkrogers
    gkrogers almost 12 years
    The /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
    Niels Brinch over 11 years
    Can 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
    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
    Niels Brinch over 11 years
    ROBOCOPY C:\Source C:\Destination *.zip /E
  • jonsinfinity
    jonsinfinity over 11 years
    Weird. 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
    Niels Brinch over 11 years
    Really!? 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
    jonsinfinity over 11 years
    I only tested ROBOCOPY C:\Source C:\Destination *.zip /E, but you should be able to include .jpg, like this ROBOCOPY C:\Source C:\Destination *.zip *.jpg /E.
  • ssinganamalla
    ssinganamalla almost 11 years
    tar cvf /tmp/full-structure.tar **/data.zip **/info.txt worked for me
  • Crashworks
    Crashworks about 10 years
    @Christian it has a dependency on cpio which isn't part of standard install.
  • user4317867
    user4317867 about 9 years
    Late 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 use popd to return to the local system. Now you can run commands external to a server without installing them.
  • Avatar
    Avatar about 8 years
    I 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 to set FILENAMES_TO_COPY=*.jpg
  • Nike
    Nike over 7 years
    Both "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
    Nike over 7 years
    Does 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
    Anfelipe almost 6 years
    The question says WINDOWS
  • JohnP
    JohnP almost 6 years
    Win10 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
    ricardo.scholz about 5 years
    I'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
    sakra about 5 years
    Try set "SOURCE_DIR=C:\Origin Test" and set "DEST_DIR=C:\Dest Test". Note the placement oft the double quotes.
  • MakotoE
    MakotoE almost 5 years
    Running 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
    Micka over 4 years
    can this work recursively, like copying all .txt from subfolders and preserving directory structure relative to the base directory?
  • Anurag S Sharma
    Anurag S Sharma almost 3 years
    @Micka yes it does
  • Anton Duzenko
    Anton Duzenko over 2 years
    How would I also delete the copied files in source dir?
  • jonsinfinity
    jonsinfinity over 2 years
    @AntonDuzenko Maybe the MOV or MOVE parameters for ROBOCOPY will help you. Run ROBOCOPY /? from a command line to see more info. Setup some test folders and files before using on anything important.