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

前端 未结 15 1681
半阙折子戏
半阙折子戏 2020-12-07 07:50

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
  folde         


        
相关标签:
15条回答
  • 2020-12-07 08:20

    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.

    1. Create your list of source files with complete paths, including drive letter if nec, in a text file.
    2. Switch on regular expressions in your text editor.
    3. Add double quotes round each line in case of spaces - search string (.*) replace string "\1", and click replace all
    4. Create two lines per file - one to create the directory, one to copy the file (qqq will be replaced with destination path) - search string (.*) replace string md qqq\1\nxcopy \1 qqq\1\n and click replace all
    5. Remove the filename from the destination paths – search \\([^\\^"]+)"\n replace \\"\n
    6. Replace in the destination path (in this example A:\src and B:\dest). Turn OFF regular expressions, search qqq"A:\src\ replace B:\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:

    md "B:\dest\a\b\c\"
    xcopy "C:\src\a\b\c\e.xyz" "B:\dest\a\b\c\e.xyz"
    

    repeated for every file in your original list. Tested on Win7.

    0 讨论(0)
  • 2020-12-07 08:24

    With find and cp only:

    mkdir /tmp/targetdir
    cd sourcedir
    find . -type f -name '*.zip' -exec cp -p --parents {} /tmp/targetdir ";"
    find . -type f -name '*.txt' -exec cp -p --parents {} /tmp/targetdir ";"
    
    0 讨论(0)
  • 2020-12-07 08:24

    To do this with drag and drop use winzip there's a dir structure preserve option. Simply create a new .zip at the directory level which will be your root and drag files in.

    0 讨论(0)
  • 2020-12-07 08:28

    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.

    0 讨论(0)
  • 2020-12-07 08:28

    Under Linux and other UNIX systems, using the tar command would do this easily.

    $ tar cvf /tmp/full-structure.tar *data.zip *info.txt
    

    Then you'd cwd to the target and:

    $ tar xvf /tmp/full-structure.tar 
    

    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 cvf -  *data.zip *info.txt |  tar xvf - 
    

    Tar (gnutar) is available on Windows too, but I'd probably use the xcopy method myself on that platform.

    0 讨论(0)
  • 2020-12-07 08:28

    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:

       rar a -r C:\Downloads\music.rar X:\music\Folder.jpg
    
    • C:\Downloads\music.rar = Archive to be created
    • X:\music\ = Folder containing music files
    • Folder.jpg = Filename I wanted to copy

    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/

    0 讨论(0)
提交回复
热议问题