if statement in MSDOS

November 23, 2010 at 00:08:07
Specs: Windows XP
set MyCount=99
set #=%MyCount%

set length=0
:loop
if defined # (set #=%#:~1%&set /A length +=1&goto loop)
echo Length is %length% characters long!

if [%length%]==1 goto _a
if [%length%]==2 goto _b

:_a
echo "one"
set newCount=00000000000000%MyCount%
echo newCount1 is %newCount%

:_b
echo "two"
set newCount=0000000000000%MyCount%
echo newCount2 is %newCount%

:end

#######################################

The result as below:- (Which is not corrected)

Length is 2 characters long
"one"
newCount1 is 0000000000000099
"two"
newCount2 is 000000000000099


It should return as below only:-

Length is 2 characters long

"two"
newCount2 is 000000000000099

Please advice on the IF and GOTO statement, anything I coded wrong?

Thanks!




See More: if statement in MSDOS

Report •

#1
November 23, 2010 at 00:59:04
This is an XP bat not DOS. But regardless, this will never resolve true:

if [%length%]==1 goto _a

you need:

if [%length%]==[1] goto _a

================================

You can save some typing with:

goto :%length%


=====================================
Life is too important to be taken seriously.

M2


Report •
Related Solutions


Ask Question