Batch File not working with IF,SET combination

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rbateman
Posts: 5
Joined: 04 Oct 2012 15:20

Batch File not working with IF,SET combination

#1 Post by rbateman » 04 Oct 2012 15:35

I have no idea why this .bat is not working. Line 40 of the code requests input from the user and assigns the value to the variable called "SECTION". If the input is not 1,2,3, or 4, the result will loop to the piece of code called :MISTAKE and display the message that the input is incorrect. If I input the numbers 3, or 4, the program will end unexpectly. Why? If you may, please also respond with a corrected fix as I am pretty new to writing batch files. Thank you.

@ECHO OFF

:START

CLS
ECHO.
ECHO Program: APR1400 RCS Forces and Moments Post Production
ECHO Programmer: rbateman
ECHO Date: October 3, 2012
ECHO.
ECHO.

IF NOT "%CONCRETE%"=="" GOTO CHECK
IF NOT "%SECTION%"=="" IF NOT "%SOIL%"=="" IF "%CONCRETE%"=="" GOTO CONCRETE_CHOICE
IF NOT "%SECTION%"=="" IF "%SOIL%"=="" GOTO SOIL_CHOICE
GOTO SECTION_CHOICE

:MISTAKE

CLS
ECHO.
ECHO Program: APR1400 RCS Forces and Moments Post Production
ECHO Programmer: Robert Bateman
ECHO Date: October 3, 2012
ECHO.
ECHO.
SET /P RETURN=%ERROR% is not a valid input. Please Try again...
CLS
GOTO START

:SECTION_CHOICE

ECHO Select a Section Calculation (1-4)
ECHO.
ECHO 1 (3.1)
ECHO 2 (3.2)
ECHO 3 (3.3 NO SURGE LINE)
ECHO 4 (3.3 SURGE LINE)
ECHO.
SET /P SECTION=Section Calculation:

IF %SECTION%==1 ( SET SECTION="3.1"
GOTO START )
IF %SECTION%==2 ( SET SECTION="3.2"
GOTO START )
IF %SECTION%==3 ( SET SECTION="3.3 NO SURGE LINE"
GOTO START )
IF %SECTION%==4 ( SET SECTION="3.3 SURGE LINE"
GOTO START )

SET ERROR=%SECTION%
SET SECTION=
GOTO MISTAKE

:SOIL_CHOICE

ECHO Select a Soil Case(1-10):
ECHO.
ECHO 1 SOIL CASE 01
ECHO 2 SOIL CASE 02
ECHO 3 SOIL CASE 03
ECHO 4 SOIL CASE 04
ECHO 5 SOIL CASE 05
ECHO 6 SOIL CASE 06
ECHO 7 SOIL CASE 07
ECHO 8 SOIL CASE 08
ECHO 9 SOIL CASE 09
ECHO 10 SOIL CASE 10
ECHO.
SET /P SOIL=Soil Case:

IF %SOIL%==1 SET SOIL=01
IF %SOIL%==2 SET SOIL=02
IF %SOIL%==3 SET SOIL=03
IF %SOIL%==4 SET SOIL=04
IF %SOIL%==5 SET SOIL=05
IF %SOIL%==6 SET SOIL=06
IF %SOIL%==7 SET SOIL=07
IF %SOIL%==8 SET SOIL=08
IF %SOIL%==9 SET SOIL=09

IF %SOIL%==01 GOTO START
IF %SOIL%==02 GOTO START
IF %SOIL%==03 GOTO START
IF %SOIL%==04 GOTO START
IF %SOIL%==05 GOTO START
IF %SOIL%==06 GOTO START
IF %SOIL%==07 GOTO START
IF %SOIL%==08 GOTO START
IF %SOIL%==09 GOTO START
IF %SOIL%==10 GOTO START
SET ERROR=%SOIL%
SET SOIL=
GOTO MISTAKE

:CONCRETE_CHOICE

ECHO Select a Concrete Type / Earthquake Motion(U, CH, CV):
ECHO.
ECHO U (Uncracked - All Directions)
ECHO CH (Cracked - Horizontal Directions)
ECHO CV (Cracked - Vertical Direction)
ECHO.
SET /P CONCRETE=Concrete Type:
IF %CONCRETE%==U GOTO START
IF %CONCRETE%==CV GOTO START
IF %CONCRETE%==CH GOTO START
SET ERROR=%CONCRETE%
SET CONCRETE=
GOTO MISTAKE

:CHECK

ECHO SECTION = %SECTION% , SOIL CASE = %SOIL% , CONCRETE TYPE = %CONCRETE%
ECHO.
SET /P PROCEED=Would you like to proceed? (Y/N)
CLS
IF %PROCEED%==N SET CONCRETE=
IF %PROCEED%==N SET SOIL=
IF %PROCEED%==N SET SECTION=
IF %PROCEED%==N GOTO START
IF %PROCEED%==Y IF %CONCRETE%==U GOTO END
IF %PROCEED%==Y IF %CONCRETE%==CV GOTO END
IF %PROCEED%==Y IF %CONCRETE%==CH GOTO END
SET ERROR=%PROCEED%
SET PROCEED=
GOTO MISTAKE

:END

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch File not working with IF,SET combination

#2 Post by Squashman » 04 Oct 2012 19:57

Kind of round about way of doing things. I cleaned up some of your code.

Code: Select all

@ECHO OFF
setlocal

:START

CLS
ECHO.
ECHO Program: APR1400 RCS Forces and Moments Post Production
ECHO Programmer: rbateman
ECHO Date: October 3, 2012
ECHO.
ECHO.

IF NOT "%CONCRETE%"=="" GOTO CHECK
IF NOT "%SECTION%"=="" IF NOT "%SOIL%"=="" IF "%CONCRETE%"=="" GOTO CONCRETE_CHOICE
IF NOT "%SECTION%"=="" IF "%SOIL%"=="" GOTO SOIL_CHOICE
GOTO SECTION_CHOICE

:MISTAKE

CLS
ECHO.
ECHO Program: APR1400 RCS Forces and Moments Post Production
ECHO Programmer: Robert Bateman
ECHO Date: October 3, 2012
ECHO.
ECHO.
SET /P RETURN=%ERROR% is not a valid input. Please Try again...
CLS
GOTO START

:SECTION_CHOICE

ECHO Select a Section Calculation (1-4)
ECHO.
ECHO 1 (3.1)
ECHO 2 (3.2)
ECHO 3 (3.3 NO SURGE LINE)
ECHO 4 (3.3 SURGE LINE)
ECHO.
SET /P S_CHOICE=Section Calculation:

IF %S_CHOICE%==1 SET "SECTION=3.1"
IF %S_CHOICE%==2 SET "SECTION=3.2"
IF %S_CHOICE%==3 SET "SECTION=3.3 NO SURGE LINE"
IF %S_CHOICE%==4 SET "SECTION=3.3 SURGE LINE"

IF DEFINED SECTION GOTO START

SET ERROR=%S_CHOICE%
SET SECTION=
GOTO MISTAKE

:SOIL_CHOICE

ECHO Select a Soil Case(1-10):
ECHO.
ECHO 1  SOIL CASE 01
ECHO 2  SOIL CASE 02
ECHO 3  SOIL CASE 03
ECHO 4  SOIL CASE 04
ECHO 5  SOIL CASE 05
ECHO 6  SOIL CASE 06
ECHO 7  SOIL CASE 07
ECHO 8  SOIL CASE 08
ECHO 9  SOIL CASE 09
ECHO 10 SOIL CASE 10
ECHO.
SET /P SOIL_CHOICE=Soil Case:
IF %SOIL_CHOICE% GTR 10 GOTO SOILERROR
IF %SOIL_CHOICE% LSS 1 GOTO SOILERROR

IF %SOIL_CHOICE% LSS 10 (
   SET SOIL=0%SOIL_CHOICE%
) ELSE (
   SET SOIL=%SOIL_CHOICE%
)      
GOTO START

:SOIL_ERROR
SET ERROR=%SOIL_CHOICE%
SET SOIL=
GOTO MISTAKE

:CONCRETE_CHOICE

ECHO Select a Concrete Type / Earthquake Motion(U, CH, CV):
ECHO.
ECHO U (Uncracked - All Directions)
ECHO CH (Cracked - Horizontal Directions)
ECHO CV (Cracked - Vertical Direction)
ECHO.
SET /P CONCRETE=Concrete Type:
IF /I %CONCRETE%==U GOTO START
IF /I %CONCRETE%==CV GOTO START
IF /I %CONCRETE%==CH GOTO START
SET ERROR=%CONCRETE%
SET CONCRETE=
GOTO MISTAKE

:CHECK

ECHO SECTION = %SECTION% , SOIL CASE = %SOIL% , CONCRETE TYPE = %CONCRETE%
ECHO.
SET /P PROCEED=Would you like to proceed? (Y/N)
CLS
IF /I %PROCEED%==N (
   SET CONCRETE=
   SET SOIL=
   SET SECTION=
   GOTO START
)
IF /I %PROCEED%==Y GOTO END
SET ERROR=%PROCEED%
SET PROCEED=
GOTO MISTAKE

:END

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch File not working with IF,SET combination

#3 Post by foxidrive » 04 Oct 2012 22:38

I hope Squashman has helped you.

I merely want to comment on debugging your original code, and showing you how to test the menu subroutine.
Here is your menu section verbatim, and here it works fine. See how I've added an echo statement in two places and pause lines to show you what is in the variable.

Run it for yourself to test it.

Code: Select all

@echo off
:start
:SECTION_CHOICE

echo.section=%section%
pause

ECHO Select a Section Calculation (1-4)
ECHO.
ECHO 1 (3.1)
ECHO 2 (3.2)
ECHO 3 (3.3 NO SURGE LINE)
ECHO 4 (3.3 SURGE LINE)
ECHO.
SET /P SECTION=Section Calculation:

IF %SECTION%==1 ( SET SECTION="3.1"
GOTO START )
IF %SECTION%==2 ( SET SECTION="3.2"
GOTO START )
IF %SECTION%==3 ( SET SECTION="3.3 NO SURGE LINE"
GOTO START )
IF %SECTION%==4 ( SET SECTION="3.3 SURGE LINE"
GOTO START )

SET ERROR=%SECTION%
SET SECTION=
echo error=%error%
pause

rbateman
Posts: 5
Joined: 04 Oct 2012 15:20

Re: Batch File not working with IF,SET combination

#4 Post by rbateman » 05 Oct 2012 01:22

Thank you for helping me. However, I still do not understand why my code fundamentally does not work when 3 is inputted. May someone explain why the line

IF %SECTION%==3 ( SET SECTION="3.3 NO SURGE LINE"
GOTO START )

Doesn't work.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch File not working with IF,SET combination

#5 Post by foxidrive » 05 Oct 2012 02:21

rbateman wrote:May someone explain why the line

IF %SECTION%==3 ( SET SECTION="3.3 NO SURGE LINE"
GOTO START )

Doesn't work.


That line works fine here, and you would have seen that it you had tried my debugging section above.


Rather than figure out your logic, try this which is top-down styled. Error checking for blank input etc is improved.

Code: Select all

@ECHO OFF
setlocal

:START

:SECTION_CHOICE
call :PRINT_HEADER

ECHO Select a Section Calculation (1-4)
ECHO.
ECHO 1 (3.1)
ECHO 2 (3.2)
ECHO 3 (3.3 NO SURGE LINE)
ECHO 4 (3.3 SURGE LINE)
ECHO.
set "Section="
set "s_choice=placeholder"
SET /P "S_CHOICE=Section Calculation: "

IF %S_CHOICE%==1 SET "SECTION=3.1"
IF %S_CHOICE%==2 SET "SECTION=3.2"
IF %S_CHOICE%==3 SET "SECTION=3.3 NO SURGE LINE"
IF %S_CHOICE%==4 SET "SECTION=3.3 SURGE LINE"

IF NOT DEFINED SECTION (
call :error
goto :SECTION_CHOICE
)


:SOIL_CHOICE
call :PRINT_HEADER

ECHO Select a Soil Case (1-10):
ECHO.
ECHO 1  SOIL CASE 01
ECHO 2  SOIL CASE 02
ECHO 3  SOIL CASE 03
ECHO 4  SOIL CASE 04
ECHO 5  SOIL CASE 05
ECHO 6  SOIL CASE 06
ECHO 7  SOIL CASE 07
ECHO 8  SOIL CASE 08
ECHO 9  SOIL CASE 09
ECHO 10 SOIL CASE 10
ECHO.
set "SOIL_CHOICE=placeholder"
SET /P "SOIL_CHOICE=Soil Case: "

if %SOIL_CHOICE% LSS 1 set "SOIL_CHOICE=ERROR"
if %SOIL_CHOICE% GTR 10 set "SOIL_CHOICE=ERROR"

if not "%SOIL_CHOICE%"=="ERROR" IF %SOIL_CHOICE% LSS 10 SET SOIL_CHOICE=0%SOIL_CHOICE%

if "%SOIL_CHOICE%"=="ERROR" (
call :error
goto :SOIL_CHOICE
)


:CONCRETE_CHOICE
call :PRINT_HEADER

ECHO Select a Concrete Type / Earthquake Motion(U, CH, CV):
ECHO.
ECHO U (Uncracked - All Directions)
ECHO CH (Cracked - Horizontal Directions)
ECHO CV (Cracked - Vertical Direction)
ECHO.
set "ConcreteType="
set "concrete=placeholder"
SET /P "CONCRETE=Concrete Type: "
IF /I %CONCRETE%==U set ConcreteType=U
IF /I %CONCRETE%==CV set ConcreteType=CV
IF /I %CONCRETE%==CH set ConcreteType=CH

IF NOT DEFINED ConcreteType (
call :error
goto :CONCRETE_CHOICE
)


:CHECK
call :PRINT_HEADER

ECHO SECTION = %SECTION% , SOIL CASE = %SOIL_CHOICE% , CONCRETE TYPE = %ConcreteType%
ECHO.
set "proceed="
SET /P "PROCEED=Would you like to proceed? (Y/N)"
CLS
IF /I %PROCEED%==N (
   SET ConcreteType=
   SET SOIL_CHOICE=
   SET SECTION=
   GOTO START
)

IF /I %PROCEED%==Y (
echo ready to go...
pause
GOTO :EOF
)

goto :eof

:PRINT_HEADER
CLS
ECHO.
ECHO Program: APR1400 RCS Forces and Moments Post Production
ECHO Programmer: rbateman
ECHO Date: October 3, 2012
ECHO.
ECHO.
goto :eof

:error
echo.
echo Your choice is invalid - try again
pause
goto :EOF

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch File not working with IF,SET combination

#6 Post by Squashman » 05 Oct 2012 06:18

I like the top down style as well. Good job on cleaning up the code. Did notice a minor error. Doesn't look like soil will ever get set to 10 if the soil choice is equal to 10.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch File not working with IF,SET combination

#7 Post by foxidrive » 05 Oct 2012 06:58

Thanks for spotting that Squashman. I edited it and altered the SOIL variable to SOIL_CHOICE.

rbateman
Posts: 5
Joined: 04 Oct 2012 15:20

Re: Batch File not working with IF,SET combination

#8 Post by rbateman » 05 Oct 2012 11:57

rbateman wrote:
May someone explain why the line

IF %SECTION%==3 ( SET SECTION="3.3 NO SURGE LINE"
GOTO START )

Doesn't work.


"That line works fine here, and you would have seen that it you had tried my debugging section above."


That line does not work fine here and you know this if you had to create a dummy variable called s_choice.

YOUR CODE:

set "Section="
set "s_choice=placeholder"
SET /P "s_choice=Section Calculation: "

IF %s_choice%==1 SET "SECTION=3.1"
IF %s_choice%==2 SET "SECTION=3.2"
IF %s_choice%==3 SET "SECTION=3.3 NO SURGE LINE"
IF %s_choice%==4 SET "SECTION=3.3 SURGE LINE"

IF NOT DEFINED SECTION (
call :error
goto :SECTION_CHOICE
)

______________________

Your code works. If I enter in 3, the program will continue. However, if you remove s_choice. and put

set "SECTION="
SET /P "SECTION=Section Calculation: "

IF %SECTION%==1 SET "SECTION=3.1"
IF %SECTION%==2 SET "SECTION=3.2"
IF %SECTION%==3 SET "SECTION=3.3 NO SURGE LINE"
IF %SECTION%==4 SET "SECTION=3.3 SURGE LINE"

IF NOT DEFINED SECTION (
call :error
goto :SECTION_CHOICE
)

___________

If I enter in 3 for that code, the bat file will end unexpectly. To reiterate, why doesn't the above work? Any help understanding this logic flaw would be greatly appreciated.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Batch File not working with IF,SET combination

#9 Post by Liviu » 05 Oct 2012 12:42

rbateman wrote:If I enter in 3 for that code, the bat file will end unexpectly. To reiterate, why doesn't the above work?

To reiterate foxidrive's advice, why not add some echo's to see what's going on?

Code: Select all

echo section=[%section%]
echo on
IF %SECTION%==4 SET "SECTION=3.3 SURGE LINE"
echo off

That will make it clear that the error is on the ==4 line, not on the ==3 one. And also that it's caused by %section% having been assigned right before with a string containing spaces. The other error case that the code you posted doesn't handle is the user simply pressing enter at the set/p prompt, which will cause a syntax error on the ==1 line.

Liviu

rbateman
Posts: 5
Joined: 04 Oct 2012 15:20

Re: Batch File not working with IF,SET combination

#10 Post by rbateman » 05 Oct 2012 13:04

rbateman wrote:
May someone explain why the line

IF %SECTION%==3 ( SET SECTION="3.3 NO SURGE LINE"
GOTO START )

Doesn't work.


"That line works fine here, and you would have seen that it you had tried my debugging section above."


That line does not work fine here and you know this if you had to create a dummy variable called s_choice.

YOUR CODE:

set "Section="
set "s_choice=placeholder"
SET /P "s_choice=Section Calculation: "

IF %s_choice%==1 SET "SECTION=3.1"
IF %s_choice%==2 SET "SECTION=3.2"
IF %s_choice%==3 SET "SECTION=3.3 NO SURGE LINE"
IF %s_choice%==4 SET "SECTION=3.3 SURGE LINE"

IF NOT DEFINED SECTION (
call :error
goto :SECTION_CHOICE
)

______________________

Your code works. If I enter in 3, the program will continue. However, if you remove s_choice. and put

set "SECTION="
SET /P "SECTION=Section Calculation: "

IF %SECTION%==1 SET "SECTION=3.1"
IF %SECTION%==2 SET "SECTION=3.2"
IF %SECTION%==3 SET "SECTION=3.3 NO SURGE LINE"
IF %SECTION%==4 SET "SECTION=3.3 SURGE LINE"

IF NOT DEFINED SECTION (
call :error
goto :SECTION_CHOICE
)

___________

If I enter in 3 for that code, the bat file will end unexpectly. To reiterate, why doesn't the above work? Any help understanding this logic flaw would be greatly appreciated.

I thank all of you for helping me with this. The proposed solutions are wonderful and I thank you for them, but please if you may, answer my question directly. If I enter in a 3 for my code, why does it not work. Instead of showing me investigative techniques involving echos (I have tried this before creating this thread) and not answering the question, please instead just answer the question. Thanks.

For the person who deleted my question last night, please if you must, delete the previous post that looks similar to this and keep this one.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch File not working with IF,SET combination

#11 Post by Squashman » 05 Oct 2012 13:12

If you are going to quote yourself in the 3rd person at least put Bulletin Board QUOTE tags around your text and put Bulletin Board CODE tags around your code. Makes it much easier to read as you can see from other peoples posts in this thread.

rbateman
Posts: 5
Joined: 04 Oct 2012 15:20

Re: Batch File not working with IF,SET combination

#12 Post by rbateman » 05 Oct 2012 13:55

To Squashman.

I do not know how to properly use the board's tools so please forgive me. All I would like is to have someone answer my question so I can say thank you. Considering that I have reposted and rephrased my question about 5 times now, I suspect that people on this board will start to get angry, and start with passive aggressive comments. I do not wish for that to happen.

Besides I found the answer to my question anyway that involved someone else in my office. The answer was never posted in this thread. Thank you for all your help.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Batch File not working with IF,SET combination

#13 Post by Liviu » 05 Oct 2012 14:00

rbateman wrote:Besides I found the answer to my question anyway that involved someone else in my office. The answer was never posted in this thread.

What wasn't clear in my answer?
Liviu wrote:That will make it clear that the error is on the ==4 line, not on the ==3 one. And also that it's caused by %section% having been assigned right before with a string containing spaces. The other error case that the code you posted doesn't handle is the user simply pressing enter at the set/p prompt, which will cause a syntax error on the ==1 line.

Liviu

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch File not working with IF,SET combination

#14 Post by foxidrive » 05 Oct 2012 23:07

He's a fool. He can't read, has trouble testing examples provided for him, and he refuses to say thank you to people who have taken their time to do work on his behalf.

To top it off he claims to to have solved it but doesn't repay the forum by describing what he found. I doubt he will return, unless he has another problem.

Post Reply