Mark Needham

Thoughts on Software Development

If Else statements in batch files

with 13 comments

I mentioned in a couple of earlier posts that I’ve been doing quite a bit of work with batch files and the windows command line, and today I wanted to do an If Else statement in one of my scripts.

I thought it would be relatively simple, but after various searches and having read articles that suggested that there wasn’t an ELSE construct in batch land I finally found a forum post which explained how to do it.

The script I’m working on takes in a working directory as one of the arguments and what I wanted to do was either set a variable to be the value passed in, or if the value passed in was ‘.’ then to set it to the current working directory.

1
2
3
4
5
IF "%1"=="."  (
  set WORKING_DIRECTORY=%cd%
) ELSE (
  set WORKING_DIRECTORY=%1
)

I played around with this a little bit and it does seem that the brackets need to be in that exact format otherwise it doesn’t work at all. Even putting brackets around the IF part of the statement will stop the script from working as expected.

IF statements on their own are much easier to deal with. To check for an empty argument for example either of the following will work:

1
2
3
IF "%1"=="" GOTO usage

IF [%1]==[] GOTO usage

It does all seem a bit fiddly and Powershell is probably the way forwards, but for now batch scripts it is!

Written by Mark Needham

August 13th, 2008 at 10:27 pm

  • Jayesh

    Can you not use cygwin?

  • http://www.markhneedham.com Mark Needham

    The reason I’m not using cygwin is because we’re using the same script to deploy stuff on different machines e.g. one for QA, one for Showcase etc and for flexibility it seemed easier just to work with the lowest denominator.

  • http://www.build-doctor.com Julian Simpson

    Mark,

    I’m with you on Cygwin where you need to redistribute the Cygwin binaries. It’s painful. I’m betting on dynamic languages.

    Once Iron Ruby has a production release I think it will be very compelling for this kind of thing.

    Something has to put batch programming to bed …

  • http://markthomas.info/blog Mark Thomas

    The tip about [] for empty args is really useful, often arguments contain spaces and so you end up with something like this which will break:

    IF “”my 1st argument”"==”" GOTO Continue

  • Anu

    What if the %cd$% resolves to a directory with brackets in it..
    IF “%1″==”.” (
    set WORKING_DIRECTORY=%cd%
    ) ELSE (
    set WORKING_DIRECTORY=%1
    )
    where %cd% resolves to say C:\abc(2)\bin

    The if condition fails..

  • Angshuman

    Hi Mark,
    I am using the if else like below,

    if Exist %loc%some_directorynul (  echo Exists ) else (  mkdir %loc%my_directory )

    which is giving me a 1kb unknown file with the name my_directory. Where I am doing the mistake could not find. Please help.

  • http://www.facebook.com/robert.schoultz Robert Schoultz

    Hello Angshuman. I just found this site and I messed around a bit and I noticed IF ELSE statements will only work if it is positioned like this (Running Windows 7 here):

    IF condition (
    do something
    ) ELSE (
    do something else
    )

    So in other words, this wont work:
    IF condition ( do something ) ELSE ( do something else )

    Batch language is picky ;).

  • http://www.facebook.com/people/Michael-Joseph-Johnson/1650897669 Michael Joseph Johnson

    I am trying to set the following condition in a batch file. The user is required to enter syntax when prompted this part works
    SET /P test=WWIDINFO Enter WWID
    then I have an if like this
    if “%test%” GTR “0″ GOTO label1(Not sure if this is what I really want but it worke because I goto label and it executes what I want
    :label1net user /domain %test% > d:%test%.txtgoto :EOF

    what I want here is to have an if that makes sure the text entered is the correct syntax
    the sytax is correct with the following
    llnnn (Where ll= letters and nnn=numbers
    llnnl is also correct (Where the last letter is either d or a)
    so my if should check this condition and if not ECHO Incorrect syntax) and return to :top ELSE goto :label

  • Cccc

    @echo:disqus  off
    echo HELLO WORLD!!!
    echo Press any key to exit.
    pause > nul
    exit

  • Falconlarat

     not trying to call anyone out but this line;
    ~
    if %was%==test (goto yes) else (goto no)
    ~
    runs with no problems on 7 and vista

  • Power5000

    i need a if if else statment is that possible or a if else if will work?

  • Nanayakkaraan

     http://www.computerhope.com/if.htm

  • Kr

    Thanks man