batch-filesetxcopy

xcopy is not recognized when variables are defined in a batch file


I am working on a batch file to copy some files from one folder to another. If I define the paths on the batch, the xcopy command won't work. If I do not define the variables the xcopy command works normally.

Example 1, this won't work:

@echo off
set pathA=C:\xx\kk
set pathB=C:\xx\mm
xcopy "%pathA%\*.doc" "%pathB%"

Example 2, this will work:

@echo off
xcopy "C:\xx\kk\*.doc" "C:\xx\mm"

The paths I am working with, are very long and so I would like to define them as variables and avoid writing them each time I use them.

Do you guys know why xcopy does not work in the Example 1 and what can I do to resolve it?

Thanks!


Solution

  • The problem was somehow the backslash when setting the variables. I have just set the variables as:

    set pathA=C:\xx\kk\ set pathB=C:\xx\mm\

    and call xcopy as:

    xcopy "%pathA%*.doc" "%pathB%" /S /E /Y

    Still no idea why, but now it works well!

    Thanks again for your help!