batch files and special characters in IF

Microsoft Windows xp professional w/serv...
July 13, 2010 at 03:36:41
Specs: Windows XP
Hi,
I have two batch files a.bat and b.bat
in a.bat I am setting a variable like:
set a="<a>"
It then calls b.bat with:
call b.bat
in b.bat I have something like:
IF "%a%"="" set a=val

But it fails with unable to read <

Please help me overcome this problem
IF


See More: batch files and special characters in IF

Report •


#1
July 13, 2010 at 13:50:38
since the var. is quoted, it might not be the <, but rather the double application of dbl-quotes.
first, you have set a to: "<a>"
then in b, you have tested the var inside quotes: "%a%"
which effectively tests against ""%a%"" which fails.
also, you either need == (old style) or equ (new style)
set your test up in b like this: if %a%=="" set a=val
or: if %a% equ "" set a=val
or, alternatively, if you prefer to strip the quotes:
set a=%a:1,-1%
and now you will need the quotes for the null-compare:
if "%a%" equ "" set a=val

Report •

#2
July 13, 2010 at 21:55:44
Hi,
Thanks for your reply.My appologies to miss "=" in the previous post:
Its actually IF "%a%"=="" set a=val
And while doing this check in second batch this error message is print in console: "< was unexpected at this time."
If I echo %a% before this check then it prints the proper value but while doing check in "IF" it fails.
Is there any alternate way of checking this null value...
Thanks for your reply.

Report •

#3
July 13, 2010 at 21:56:55
Hi,
Thanks for your reply.
My apologies to miss "=" in the previous post:

Its actually IF "%a%"=="" set a=val

And while doing this check in second batch this error message is print in console: "< was unexpected at this time."

If I echo %a% before this check then it prints the proper value but while doing check in "IF" it fails.

Is there any alternate way of checking this null value...
Thanks for your reply.


Report •

Related Solutions

#4
July 13, 2010 at 23:37:52
i think you missed the gist (no poetry intended lol!).
If you assign a var, (f/e: a) a value "<a>" then that variable will
hold the quotes and be exactly this: "<a>"
not <a>
then downstream in the program, it will always look like exactly this: "<a>"
so if you IF test the variable so:
if "%a%"
since %a% is equal to exactly literally:"<a>"
and not <a>
then if you put quotes around it in the IF stmt, you get
garbage like: IF ""<a>"" equ ""

see what i mean? if not, i give up...


Report •

#5
July 14, 2010 at 01:10:37
so if you IF test the variable so:
if "%a%"
since %a% is equal to exactly literally:"<a>"
and not <a>
then if you put quotes around it in the IF stmt, you get
garbage like: IF ""<a>"" equ ""

see what i mean? if not, i give up...

Actually in my case its not only double quotes its like:
set a="<2> a" b

then when its value is verified inside IF like:
IF "%a%" == "" then it fails.
Otherwise if I escape it like:
IF "%%a%" == "" (extra %)
it works with non null value("<2> a"b) but if it was set to null then this check would not be performed at all...
Any comments???
Thanks for your suggestions...


Report •

#6
July 14, 2010 at 01:49:42
not sure, but maybe "if defined", which is somewhat equivalent to: IF %var% NEQ ""

if defined a (call :one) else (goto :eof)

if var a is indeed null (not "" or any other blather) , this should work.


Report •

#7
July 14, 2010 at 02:48:22
Hey Nbrane that works !!!!

Thanks for your valuable suggestion.....

I replaced IF "%a%" == "" with IF NOT DEFINED a
and it works !!!

Thanks again for your help......


Report •

Ask Question