DownToo
DownToo

Reputation: 1

Xcopy change source folder if doesn't exist

I don't have any problem to do Xcopy, but the source folder may be not the one specified. It could be one among three folders.

So I would like a proceed that

set sourcefolder = "folder1"
if not exist set sourcefolder = "folder2"
if not exist set sourcefolder = "folder3"
if not exist exit

But I'm a newbie in scripting, so I would be glad if anyone could help me please.

Upvotes: 0

Views: 576

Answers (1)

aschipfl
aschipfl

Reputation: 34909

There are some issues in your code:

  • do not put spaces around the = in a set command line, because set sourcefolder = path assigns SPACEpath to the variable sourcefolderSPACE;
  • use the set syntax set "sourcefolder=C\MyFolder"; the quotes "" do not become part of the variable value here, so place them when expanding (reading) the variable like "%sourcefolder%";
  • after if not exist you need to specify what to check, like if not exist "%sourcefolder%";
  • to end a batch file use exit /B rather than exit as the latter terminates also the command prompt (cmd) instance;

Here is the fixed code:

set "sourcefolder=C:\MyFolder"
if not exist "%sourcefolder%" set "sourcefolder=D:\MyFolder"
if not exist "%sourcefolder%" set "sourcefolder=E:\MyFolder"
if not exist "%sourcefolder%" (
    echo Could not find a valid source folder.
    exit /B
)
xcopy /E /I /Y "%sourcefolder%" "V:\Save\"

Upvotes: 1

Related Questions