IF Statement not Working

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Fireball
Posts: 4
Joined: 03 Nov 2013 14:11

IF Statement not Working

#1 Post by Fireball » 03 Nov 2013 14:25

This is my (probably too complex and can be simplified a thousand times) code:

Code: Select all

:DECLARE_UNLOCK
  cls
  set i=0
  set y=0
  for /F "tokens=*" %%A in (%USERPROFILE%\fields.txt) do (
    SET /A i+=1
    set field!i!=%%A
  )
  for /F "tokens=*" %%A in (%USERPROFILE%\fieldtype.txt) do (
    SET /A y+=1
    set fieldtype!y!=%%A
  )
  set x=0
  set y=1
  set i=0
  goto UNLOCK
 
:UNLOCK
  cls
  if %i%==%numberofields% goto SHOW_FOLDER
  set /a y+=1
  set /a i+=1
  if !fieldtype%y%!==1 (
    set /p enter%y%=Enter your name:
    if !enter%y%!==!field%i%! goto UNLOCK
    goto INTRUDER
  )


I'm pretty sure the problem is the IF statement (I'm not 100% sure because there's no error). It's almost like it's skipping the IF, even though I know that the

Code: Select all

!fieldtype%y%!==1
. Don't worry, I've already

Code: Select all

 setlocal EnableDelayedExpansion and EnableExtansions
. I've tried debugging it over a hundred different ways (making sure the variables are set, echoing everything, trying examples), and I've been to every corner of the web trying to figure it out. Any help would be greatly appreciated!

einstein1969
Expert
Posts: 976
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: IF Statement not Working

#2 Post by einstein1969 » 03 Nov 2013 14:57

try if this work:

Code: Select all

@echo off
setlocal EnableDelayedExpansion & setlocal EnableExtensions

set y=0

    SET /A y+=1
    set fieldtype!y!=1

 set y=1

 set /a y+=1

if !fieldtype%y%!==1 (echo 1) else echo ????


result:

Code: Select all

????


Code: Select all

@echo off
setlocal EnableDelayedExpansion & setlocal EnableExtensions

set y=1

    SET /A y+=1
    set fieldtype!y!=1

 set y=1

 set /a y+=1

if !fieldtype%y%!==1 (echo 1) else echo ????


result:

Code: Select all

1


Einstein1969

Fireball
Posts: 4
Joined: 03 Nov 2013 14:11

Re: IF Statement not Working

#3 Post by Fireball » 03 Nov 2013 17:49

What's so different? (By the way, it works when I set fieldtype!y! to 1, making the whole thing make even less sense. Even more confusing, I tried echoing !fieldtype%y%! and it showed 1, but it still blew over the if statement!)

Code: Select all

 echo !fieldtype%y%!
  pause
  if !fieldtype%y%!==1 (
    echo Y: %y%
    echo I: %i%
    set /p enter%y%=Enter your name:
    echo !enter%y%!
    echo !field2!
    pause
      if !enter%y%!==!field%i%! goto UNLOCK
      goto INTRUDER
  )

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: IF Statement not Working

#4 Post by aGerman » 03 Nov 2013 18:00

I tried echoing !fieldtype%y%! and it showed 1

Try ...

Code: Select all

echo "!fieldtype%y%!"

... to see if there are any leading or trailing spaces.
If so, tell us how you created the files from where you read the data.

Regards
aGerman

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: IF Statement not Working

#5 Post by ShadowThief » 03 Nov 2013 19:57

Try using equ instead of ==.

Fireball
Posts: 4
Joined: 03 Nov 2013 14:11

Re: IF Statement not Working

#6 Post by Fireball » 03 Nov 2013 20:29

Yeah, it returns "1 ". How would I get rid of this space. I've tried !fieldtype%y%:~0,1! but it says:
1!==1 was unexpected at this time.
Does this help?

Fireball
Posts: 4
Joined: 03 Nov 2013 14:11

Re: IF Statement not Working

#7 Post by Fireball » 03 Nov 2013 21:14

I GOT IT!!! I figured out that to make "~0,1" work, I just had to put it in quotation marks like so:

Code: Select all

  if "!fieldtype%y%:~0,1!"=="1" (
    set /p enter%y%=Enter your name:
    if "!enter%y%! "=="!field%i%!" goto UNLOCK
    goto INTRUDER
  )

Thanks you guys, I wouldn't have been able to do it without you!

Greatly Appreciated,
Fireball

Post Reply