How do I copy file and folder structure using xcopy from a text file?

后端 未结 1 459
日久生厌
日久生厌 2021-01-20 07:34

I have a text file containing a list of files and folders. What I want to do is use xcopy to replicate what is written in the text file. My text file looks like this:

相关标签:
1条回答
  • 2021-01-20 08:08

    Yes, you are close. Just need to use the existing path as the appended destination path.

    Update

    @echo off
    for /f "delims=" %%A in (textfile.txt) do if exist "%%~fA\*" (
        md "C:\Output\%%~pA"
        copy /y "%%~fA" "C:\Output\%%~pnxA"
    )
    

    Original

    If %%A = "C:\Folder\Folder2\File3.txt", then %%~pA = Folder\Folder2

    @echo off
    for /f "delims=" %%A in (textfile.txt) do (
        md "C:\Output\%%~pA"
        if not exist "%%~fA\*" echo f | xcopy "%%~fA" "C:\Output\%%~pnxA" /y
    )
    

    The if not exist "%%~fA\*" makes sure to only copy the entry if it is not a directory. See Reference for more Techniques and Comments

    Type in for /? at the command line to view a list of the variable modifiers. %%~A will remove the surrounding quotations (if any) from the variable.

    Post about xcopy prompting issue. and fix #2.

    Alternate Setup, since you most likely will not need the xcopy abilities.

    @echo off
    for /f "delims=" %%A in (textfile.txt) do (
        md "C:\Output\%%~pA"
        if not exist "%%~fA\*" copy /y "%%~fA" "C:\Output\%%~pnxA"
    )
    
    0 讨论(0)
提交回复
热议问题