Batch file to copy directories recursively

前端 未结 4 1690
情书的邮戳
情书的邮戳 2020-12-01 00:40

Is there a way to copy directories recursively inside a .bat file? If so, an example would be great. thanks.

相关标签:
4条回答
  • 2020-12-01 00:53

    I wanted to replicate Unix/Linux's cp -r as closely as possible. I came up with the following:

    xcopy /e /k /h /i srcdir destdir

    Flag explanation:

    /e Copies directories and subdirectories, including empty ones.
    /k Copies attributes. Normal Xcopy will reset read-only attributes.
    /h Copies hidden and system files also.
    /i If destination does not exist and copying more than one file, assume destination is a directory.


    I made the following into a batch file (cpr.bat) so that I didn't have to remember the flags:

    xcopy /e /k /h /i %*

    Usage: cpr srcdir destdir


    You might also want to use the following flags, but I didn't:
    /q Quiet. Do not display file names while copying.
    /b Copies the Symbolic Link itself versus the target of the link. (requires UAC admin)
    /o Copies directory and file ACLs. (requires UAC admin)

    0 讨论(0)
  • 2020-12-01 00:54

    Look into xcopy, which will recursively copy files and subdirectories.

    There are examples, 2/3 down the page. Of particular use is:

    To copy all the files and subdirectories (including any empty subdirectories) from drive A to drive B, type:

    xcopy a: b: /s /e

    0 讨论(0)
  • 2020-12-01 00:54

    You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:

    @echo off
    call :treeProcess
    goto :eof
    
    :treeProcess
    rem Do whatever you want here over the files of this subdir, for example:
    copy *.* C:\dest\dir
    for /D %%d in (*) do (
        cd %%d
        call :treeProcess
        cd ..
    )
    exit /b
    

    Windows Batch File Looping Through Directories to Process Files?

    0 讨论(0)
  • 2020-12-01 01:00

    After reading the accepted answer's comments, I tried the robocopy command, which worked for me (using the standard command prompt from Windows 7 64 bits SP 1):

    robocopy source_dir dest_dir /s /e
    
    0 讨论(0)
提交回复
热议问题