Search code examples
windowsbatch-filexcopyrobocopy

Copy a directory with xcopy/robocopy


I have a batch script which takes a directory path as parameter. Inside the script, I'd like to copy this directory somewhere else.

For example, let the script parameter be "C:\Users\Raffaele\Foo" and the copy destination be "C:\Foe". At the end, I'd like to have "C:\Foe\Foo". Instead, the best I can get (both using xcopy and robocopy) is all files and subdirectories inside "Foo" copied into "Foe".


Solution

  • xcopy is good enough for your requirements, read HELP XCOPY and HELP CALL and try

    call :docopy c:\users\rafaele\foo c:\foe
    goto :eof
    :docopy
    xcopy /S /E /I %1 %2\%~n1
    goto :eof
    

    the trick is to extract the directory name and use it to specify both source and destination directories

    the /S flag copies the directories inside source

    the /E flag creates directories in destination if they exist but are empty in source

    the /I flag assumes the destination is a directory and creates it