0

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

1 Answer 1

1

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

2
  • I've thought something similar. Since "C:\Users\Raffaele\Foo" is a variable path, how can I extract just the "Foo" part? Does exist a command like "basename" in Linux?
    – TaaTT4
    May 26, 2014 at 9:06
  • yes, you can use %~n to extract the name of the directory. Answer edited.
    – PA.
    May 26, 2014 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.