Search code examples
windowsbatch-filecmdrobocopy

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


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

Solution

  • 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.