Chüngel
Chüngel

Reputation: 385

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!

Upvotes: 0

Views: 3051

Answers (2)

Miki
Miki

Reputation: 1

Make sure you don't have a space char at the end of the set command. In case you do, %pathA% resolves to C:\xx\kk\ *.doc, instead of C:\xx\kk\*.doc

Upvotes: 0

Chüngel
Chüngel

Reputation: 385

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!

Upvotes: 1

Related Questions