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

Author Topic: Help with .bat and IF / ELSE commands  (Read 28979 times)

0 Members and 1 Guest are viewing this topic.

AlexC

    Topic Starter


    Newbie

    Help with .bat and IF / ELSE commands
    « on: May 09, 2008, 07:59:57 AM »
    Hey all

    I'm trying to test a variable and if its correct open up another batch file, if its false go to another part of the original .bat file. Below is how Ive got my commands :

    IF %VARIABLE% == 1 (
         CALL TEST.BAT
    )ELSE(
         GOTO TEST2
    )

    But it generates this output without running the 2nd .bat file. prompt outputs "ELSE( was unexpected at this time". Ive tried changing the format but I cant get it to work.

    Would this be the right way of doing it, or is there a better way.

    Thanks, Alex

    blastman



      Hopeful

      Re: Help with .bat and IF / ELSE commands
      « Reply #1 on: May 09, 2008, 08:30:06 AM »
      try;

      if /i "%variable%" EQU "1" (start test.bat) else goto test2


      notice I added "/i" this will make the if statement compare them as integers. I've also added the " " around the values we're comparing and I stuck it on one line cos I reakon it's easier to read.

      I haven't tested it, but I hope it helps.

      Blastman, you are the man. Thank You Very Much!!!!!!!!!



      Dias de verano

      • Guest
      Re: Help with .bat and IF / ELSE commands
      « Reply #2 on: May 09, 2008, 10:50:00 AM »
      notice I added "/i" this will make the if statement compare them as integers.

      No it won't; using if with the /i switch makes the comparison case-insensitive, or makes it ignore case if you prefer. Since numerical digits don't have "upper" or "lower" case, it will have no effect whatsoever.

      Quote
      I've also added the " " around the values we're comparing

      Good practice

      Quote
      and I stuck it on one line cos I reakon it's easier to read.

      Ditto

      This is an alternative

      Code: [Select]
      if "%variable%"=="1" (start test.bat) else goto test2

      blastman



        Hopeful

        Re: Help with .bat and IF / ELSE commands
        « Reply #3 on: May 09, 2008, 01:41:19 PM »
        notice I added "/i" this will make the if statement compare them as integers.

        No it won't; using if with the /i switch makes the comparison case-insensitive, or makes it ignore case if you prefer. Since numerical digits don't have "upper" or "lower" case, it will have no effect whatsoever.


        I may have miss understood the explaintion for the help option.

        "and the /I switch, if specified, says to do case insensitive string compares. The /I switch can also be used on the string1==string2 form of IF."

        and the bit that confused me...

        "These comparisons are generic, in that if both string1 and string2 are both comprised of all numeric digits, then the stings are converted to numbers and a numeric comparison is performed"

        I guess I thought that last section was part of the explation of the /I switch.

        oh well.

        as long as it works eh???


        Blastman, you are the man. Thank You Very Much!!!!!!!!!



        Dias de verano

        • Guest
        Re: Help with .bat and IF / ELSE commands
        « Reply #4 on: May 09, 2008, 02:13:21 PM »
        the bit that confused me...

        "These comparisons are generic, in that if both string1 and string2 are both comprised of all numeric digits, then the stings are converted to numbers and a numeric comparison is performed"

        That means decimal (or lesser base) integers (the only numbers that don't contain punctuation or letters) are compared as numbers, everything else as strings.  (Numeric comparisons are made as if all
        the numbers were base ten, so comparing an octal and a decimal number would often give wrong results.)

        AlexC

          Topic Starter


          Newbie

          Re: Help with .bat and IF / ELSE commands
          « Reply #5 on: May 12, 2008, 03:17:27 AM »
          Thanks for the reply guys and it works  ;D