+ Post New Thread
Results 1 to 7 of 7
Scripts Thread, Can one IF command be used with multiple conditions? I.E. "If x OR y OR z goto :a"? in Coding and Web Development; Basically, exactly what the title says. Is it possible? I know I could just use Code: if x goto :a ...
  1. #1

    Join Date
    Jan 2012
    Posts
    140
    Thank Post
    24
    Thanked 11 Times in 10 Posts
    Rep Power
    5

    Question Can one IF command be used with multiple conditions? I.E. "If x OR y OR z goto :a"?

    Basically, exactly what the title says.

    Is it possible?
    I know I could just use
    Code:
    if x goto :a
    if y goto :a
    if z goto :a
    but I'm basically trying to shorten/condense as much as possible, so if it could all be done on one line, that'd be great.

    edit: Using standard batch files/command line
    Last edited by Garacesh; 31st January 2012 at 04:31 PM.

  2. IDG Tech News

  3. #2
    OB1
    OB1 is online now

    OB1's Avatar
    Join Date
    Sep 2011
    Location
    Leeds
    Posts
    104
    Thank Post
    6
    Thanked 35 Times in 29 Posts
    Rep Power
    13
    What language are you using? Most support this, usually with a | (pipe) or an 'or'

  4. #3
    DT2
    DT2 is offline

    DT2's Avatar
    Join Date
    May 2011
    Location
    Leeds
    Posts
    486
    Thank Post
    62
    Thanked 97 Times in 84 Posts
    Rep Power
    52
    Code:
    elseif
    ?

    Would this perhaps do as you require.

  5. #4

    Join Date
    Jan 2012
    Posts
    140
    Thank Post
    24
    Thanked 11 Times in 10 Posts
    Rep Power
    5
    Oh, sorry.. Language? I don't know. But it's a .bat, so just standard Command Line.
    Probably should have mentioned that xD DERP!

  6. #5

    Rawns's Avatar
    Join Date
    Jun 2009
    Location
    Taunton, Somerset
    Posts
    1,300
    Thank Post
    578
    Thanked 107 Times in 75 Posts
    Rep Power
    126
    You can do an else command for just two options:

    Code:
    IF X (GOTO A) ELSE (GOTO B)
    Performs conditional processing in batch programs.

    IF [NOT] ERRORLEVEL number command
    IF [NOT] string1==string2 command
    IF [NOT] EXIST filename command

    NOT Specifies that Windows XP should carry out
    the command only if the condition is false.

    ERRORLEVEL number Specifies a true condition if the last program run
    returned an exit code equal to or greater than the number
    specified.

    string1==string2 Specifies a true condition if the specified text strings
    match.

    EXIST filename Specifies a true condition if the specified filename
    exists.

    command Specifies the command to carry out if the condition is
    met. Command can be followed by ELSE command which
    will execute the command after the ELSE keyword if the
    specified condition is FALSE

    The ELSE clause must occur on the same line as the command after the IF. For
    example:

    IF EXIST filename. (
    del filename.
    ) ELSE (
    echo filename. missing.
    )

    The following would NOT work because the del command needs to be terminated
    by a newline:

    IF EXIST filename. del filename. ELSE echo filename. missing

    Nor would the following work, since the ELSE command must be on the same line
    as the end of the IF command:

    IF EXIST filename. del filename.
    ELSE echo filename. missing

    The following would work if you want it all on one line:

    IF EXIST filename. (del filename.) ELSE echo filename. missing

    If Command Extensions are enabled IF changes as follows:

    IF [/I] string1 compare-op string2 command
    IF CMDEXTVERSION number command
    IF DEFINED variable command

    where compare-op may be one of:

    EQU - equal
    NEQ - not equal
    LSS - less than
    LEQ - less than or equal
    GTR - greater than
    GEQ - greater than or equal

    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. These comparisons are generic, in that if both string1 and
    string2 are both comprised of all numeric digits, then the strings are
    converted to numbers and a numeric comparison is performed.

    The CMDEXTVERSION conditional works just like ERRORLEVEL, except it is
    comparing against an internal version number associated with the Command
    Extensions. The first version is 1. It will be incremented by one when
    significant enhancements are added to the Command Extensions.
    CMDEXTVERSION conditional is never true when Command Extensions are
    disabled.

    The DEFINED conditional works just like EXISTS except it takes an
    environment variable name and returns true if the environment variable
    is defined.

    %ERRORLEVEL% will expand into a string representation of
    the current value of ERRORLEVEL, provided that there is not already
    an environment variable with the name ERRORLEVEL, in which case you
    will get its value instead. After running a program, the following
    illustrates ERRORLEVEL use:

    goto answer%ERRORLEVEL%
    :answer0
    echo Program had return code 0
    :answer1
    echo Program had return code 1

    You can also using the numerical comparisons above:

    IF %ERRORLEVEL% LEQ 1 goto okay

    %CMDCMDLINE% will expand into the original command line passed to
    CMD.EXE prior to any processing by CMD.EXE, provided that there is not
    already an environment variable with the name CMDCMDLINE, in which case
    you will get its value instead.

    %CMDEXTVERSION% will expand into a string representation of the
    current value of CMDEXTVERSION, provided that there is not already
    an environment variable with the name CMDEXTVERSION, in which case you
    will get its value instead.
    Last edited by Rawns; 31st January 2012 at 04:43 PM.

  7. #6

    Join Date
    Jan 2012
    Posts
    140
    Thank Post
    24
    Thanked 11 Times in 10 Posts
    Rep Power
    5
    Yeah, I found a fair few websites like that, but none of them really helped amazingly (although the info was indeed useful, just not currently relevant)
    So if I have more than two it's not possible?

  8. #7

    Rawns's Avatar
    Join Date
    Jun 2009
    Location
    Taunton, Somerset
    Posts
    1,300
    Thank Post
    578
    Thanked 107 Times in 75 Posts
    Rep Power
    126
    Not to my knowledge.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 30
    Last Post: 27th August 2011, 09:25 AM
  2. Can the unifi 35 projector be used w/out the smartboard?
    By MercuryCrest in forum AV and Multimedia Related
    Replies: 5
    Last Post: 26th August 2011, 02:11 AM
  3. Replies: 15
    Last Post: 20th July 2011, 01:37 PM
  4. Replies: 43
    Last Post: 6th May 2011, 08:06 PM
  5. Terminal Server - limit which computers can be used to logon
    By broc in forum Thin Client and Virtual Machines
    Replies: 5
    Last Post: 26th June 2007, 02:21 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •