0
::Special Feature Available
SET testAvailable=1

::Copy a needed module
SET modulName=Module
XCOPY %moduleName%\%targetHardware%\%moduleName%\Debug\%modulName%.bin %target%

::Copy additional module if needed
IF %test1Available%==1 (
SET moduleName=TestModule1
XCOPY %moduleName%\%targetHardware%\%moduleName%\Debug\%modulName%.bin %target%
)

I am trying to copy different files from a folder-structure into another folder. Everything works fine until I try to add "special features", which I set before. (Set TestAvailable=1 or 0).

Now the batch runs into the if statement and even runs the xcopy ... But with the moduleName from before (Module instead TestModule), copying the same file again.

If I add another IF with TestModule2, the script will copy TestModule1 instead of TestModule2. And so on.

It is working if I use:

IF %Test1Available==1 SET ...
IF %Test1Available==1 XCOPY ...

I already tried to add

SETLOCAL enableDelayedExpansion

without any luck.

Why won't the variables inside the IF-statement be updated and instead it uses the old variables from before the IF-statement?

  • 1
    `setlocal EnableDelayedExpansion` alone won't help, it just *enables* delayed expansion; you'll need to use `!VAR!` instead of `%VAR%` to actually *use* it… – aschipfl Apr 05 '22 at 13:58
  • Stephan's DELAYEDEXPANSION link: https://stackoverflow.com/a/30284028/2128947 – Magoo Apr 05 '22 at 14:03
  • Thanks, this is something I have over read. Now it is working! – stimmenhotel Apr 05 '22 at 14:12

0 Answers0