简体   繁体   中英

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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM