1.

Solve : Batch file help??

Answer»

I ALREADY POSTED this in the programming section by mistake, sorry. While I do appreciate Sidwinder's help, it just didn't work.

I haven't worked with batch files in years and this problem is really irritating me.

set var1=oldvalue

if not defined %var1% set var2=newvalue


Now I would think that because var1 is defined in the first line, that var2 wouldn't GET set, but it does.


if defined %var1% set var2=newvalue


There, I would think the value would be set, right? Nope.

Anyone have any ideas on what I'm doing WRONG?

I've tried:

if not exist
if exist
if not "%var1%"==""

Stumped....

I thought we solved this.

Lose the % signs around var1 in the not defined INSTRUCTION. The name of the variable is var1 not %var1%. Use the % signs when you want to extract the value of the variable. By using the % signs, the instruction resolves as if not defined oldvalue set var2=newvalue which causes var2 to be set because presumably oldvalue is not defined. You have turned oldvalue into a variable name not a variable value.

Code: [Select]C:\Temp>set var1=

C:\Temp>set var2=

C:\Temp>set var1=oldvalue

C:\Temp>if not defined var1 set var2=newvalue

C:\Temp>set var1
var1=oldvalue

C:\Temp>set var2
Environment variable var2 not defined

var1 is defined as having a value of oldvalue therefore var2 does not get set by the if not defined statement.

After 4 years in the army, you'd think I would have better attention to detail. The % sign was the problem. Thanks again sidewinder and I'm sorry for you having to explain it to me twice.



Discussion

No Comment Found