Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch scripting help  (Read 3525 times)

0 Members and 1 Guest are viewing this topic.

Yes

    Topic Starter


    Greenhorn

    Batch scripting help
    « on: April 25, 2008, 06:31:26 PM »
    I'm trying to write a batch file that will display a list to the user, have them enter their choice, and then do something.  Here's what I have now:

    Code: [Select]
    @echo off
    echo 1. BL (main)
    echo 2. BL (sound)
    echo 3. MS-DOS

    choice /C:123 /N
    set os=%errorlevel%

    if "%os%"=="1" echo hello1
    if "%os%"=="2" echo hello2
    if "%os%"=="3" echo hello3

    The problem is that nothing is put into os.  When I try printing errorlevel after the choice line it doesn't print anything, but shouldn't it be equal to the user's choice?  I'm using MS-DOS 6, if it matters.

    Thanks!

    Dusty



      Egghead

    • I could if she would, but she won't so I don't.
    • Thanked: 75
    • Experience: Beginner
    • OS: Windows XP
    Re: Batch scripting help
    « Reply #1 on: April 25, 2008, 08:53:59 PM »
    Errorlevel is not set as an Environment Variable but as a Return Code.  You must set the Return Code as an Env Var if you wish to test it.

    So you could use:
    Code: [Select]
    @echo off
    echo 1. BL (main)
    echo 2. BL (sound)
    echo 3. MS-DOS

    choice /C:123 /N
    if errorlevel 3 set errorlev=3
    if errorlevel 2 set errorlev=2
    if errorlevel 1 set errorlev=1

    From here on the Return Code is available in the Environment Variable %errorlev%

    This is worth reading.

    Good luck
    One good deed is worth more than a year of good intentions.

    Yes

      Topic Starter


      Greenhorn

      Re: Batch scripting help
      « Reply #2 on: April 25, 2008, 09:23:36 PM »
      Ah, ok.  Unfortunately, it's still not working - with your code it printing errorlev with 'echo %errorlev%' always prints 1.

      Thanks.

      Dias de verano

      • Guest
      Re: Batch scripting help
      « Reply #3 on: April 26, 2008, 12:06:01 AM »
      Quote
      echo %errorlev%' always prints 1.

      There is a reason for this. Errorlevel 1 means "errorlevel 1 or greater" so the test will always be true, so gotos are needed if 3 or 2 are chosen

      @echo off
      echo 1. BL (main)
      echo 2. BL (sound)
      echo 3. MS-DOS

      choice /C:123 /N

      if errorlevel 3 goto choice3
      if errorlevel 2 goto choice2
      if errorlevel 1 goto choice1

      :choice3

      echo you chose 3
      goto next

      :choice2

      echo you chose 2
      goto next

      :choice1

      echo you chose 1

      :next



      Dusty



        Egghead

      • I could if she would, but she won't so I don't.
      • Thanked: 75
      • Experience: Beginner
      • OS: Windows XP
      Re: Batch scripting help
      « Reply #4 on: April 26, 2008, 12:07:34 AM »
      Sorry, I had a brain malfunction...

      Reverse the order of the IF statements to be 1 2 3 not 3 2 1

      Here's another version with a couple of embellishments:

      Code: [Select]
      @echo off
      cls
      echo.
      echo.
      echo                             1. BL (main)
      echo                             2. BL (sound)
      echo                             3. MS-DOS
      echo.
      echo.
      choice /C:123 /N "                          Enter your choice..... "
      cls

      if errorlevel 1 set errorlev=1
      if errorlevel 2 set errorlev=2
      if errorlevel 3 set errorlev=3

      echo.
      echo.
      echo                             Return Code = %errorlev%


      Hope that fixes it.
      One good deed is worth more than a year of good intentions.

      Yes

        Topic Starter


        Greenhorn

        Re: Batch scripting help
        « Reply #5 on: April 26, 2008, 06:31:00 AM »
        Great, that worked.

        Thanks!

        Dusty



          Egghead

        • I could if she would, but she won't so I don't.
        • Thanked: 75
        • Experience: Beginner
        • OS: Windows XP
        Re: Batch scripting help
        « Reply #6 on: April 26, 2008, 03:17:09 PM »
        You're welcome & again sorry about giving you false info to start with.

        Good luck
        One good deed is worth more than a year of good intentions.