1

The problem is that my set tap=c:\ca\sf\1st 2nd... etc. isn't working at all. The echo shows nothing, the set isn't putting the path in the variable for some reason. I got all ifs right, is there another problem?

setlocal enabledelayedexpansion if NEWYORK == %region% ( set tap=C:\ny CALL :process %1 %2 %tap% %cl% GOTO :EOF ) if California == %region% ( if '%3'=='sanfrancisco' ( set cl=c:\ca\sf\cl if '%2'=='1st' set tap=c:\ca\sf\1st if '%2'=='2nd' set tap=c:\ca\sf\2nd if '%2'=='3rd' set tap=c:\ca\sf\3rd if '%2'=='4th' set tap=c:\ca\sf\4th if '%2'=='5th' set tap=c:\ca\sf\5th echo %tap%, echo %cl%, pause CALL :process %1 %2 %tap% %cl% GOTO :EOF ) if '%3' == 'LosAngeles' ( set tap=c:\ca\la set cl=c:\ca\la\cl echo %tap%, %cl% pause CALL :process %1 %2 %tap% %cl% GOTO :EOF ) set tap=c:\USA set cl=c:\usa\cl echo %tap%, %cl% pause CALL :process %1 %2 %tap% %cl% GOTO :EOF ) else ( echo faiiiiiiiiiillllllllll pause GOTO :END) endlocal GOTO :EOF 
4
  • what happens if you run the script without enabledelayedexpansion? and whats your ":process"? Commented Oct 28, 2011 at 19:24
  • If the set and the echo don't work, most likely that whole branch isn't executed. Commented Oct 28, 2011 at 20:55
  • everything else is working but the "set = tap=c:\ca\sf\1st 2nd 3rd etc... without the enabledelayed its not working at all, my :process is another code else where, its working well if i dont put this part of the code.. Commented Oct 31, 2011 at 13:20
  • As Aacini wrote you need to use the delayed expansion, instead of CALL :process %1 %2 %tap% %cl% you need CALL :process %1 %2 !tap! %cl% Commented Oct 31, 2011 at 14:09

1 Answer 1

2

You missed the first SET command. The line

tap=C:\ny 

must be

set tap=C:\ny 

When you use a variable that is modified inside an IF or FOR its value must be expanded with !var! and not with %var%; otherwise the expanded value is the value the variable had BEFORE enter the IF or FOR (this is the objective of EnableDelayedExpansion). For example:

set var=Old value if 1 == 1 ( set var=New value echo With percent: %var%. With exclamation: !var! ) 

Previous segment show: With percent: Old value. With exclamation: New value

An additional comment:

Although if NEWYORK == %region% is the same as if %region% == NEWYORK when it is executed, the second one is customary and clearer from programmers point of view.

EDIT

I slightly modified your code. Take a look at it:

 setlocal enabledelayedexpansion if /I %region% == NEWYORK ( set tap=C:\ny REM cl IS NOT DEFINED HERE, BUT USED IN NEXT LINE CALL :process %1 %2 !tap! !cl! GOTO :EOF ) if /I %region% == California ( if /I '%3' == 'sanfrancisco' ( set cl=c:\ca\sf\cl set tap=c:\ca\sf\%2 echo !tap!, !cl! pause CALL :process %1 %2 !tap! !cl! GOTO :EOF ) if /I '%3' == 'LosAngeles' ( set tap=c:\ca\la set cl=c:\ca\la\cl echo !tap!, !cl! pause CALL :process %1 %2 !tap! !cl! GOTO :EOF ) set tap=c:\USA set cl=c:\usa\cl echo !tap!, !cl! pause CALL :process %1 %2 !tap! !cl! GOTO :EOF ) else ( echo faiiiiiiiiiillllllllll pause GOTO :END ) endlocal GOTO :EOF 
Sign up to request clarification or add additional context in comments.

1 Comment

ya tried it already, problem isnt with the ! or % its with the "set" in the "if"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.