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

前端 未结 15 1721
半阙折子戏
半阙折子戏 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.

提交回复
热议问题