1

I am working on some batch script with following code block:

echo off 
echo SETTING UP ANT FOR THE BUILD ....
set ANT_HOME=%~dp0%build\apache-ant-1.8.2
set ANT_BIN=%~dp0%build\apache-ant-1.8.2\bin

SET path=%path%;%ANT_BIN%;%ANT_BIN%;

echo PATH: %path%
echo ANT_HOME: %ANT_HOME%
echo ANT_BIN: %ANT_BIN%
echo ANT GOT INSTALLED ....

It help me to set up my path to my ant path in environment variable.

Now, each time i run this bat file it goes for setting up the environment variable for ant.

I have to put some condition that if path is already there then just skip the setup option else setup option begins.

How can we do this using bat script? As i am new to bat script i tried with following code block:

IF "%ANT_HOME%" == "" GOTO NOPATH :YESPATH ...

:NOPATH ...

but the problem here is i am not able to get how to check the case for following: The %ANT_HOME% is not blank but have some invalid ant directory path.

Can anyone help me out here ?

labilbe
  • 3,501
  • 2
  • 29
  • 34
Ashwin Hegde
  • 1,733
  • 4
  • 19
  • 49
  • possible duplicate of [How to verify if a file exists in a Windows .BAT file?](http://stackoverflow.com/questions/3022176/how-to-verify-if-a-file-exists-in-a-windows-bat-file) – sashoalm May 30 '13 at 06:18

3 Answers3

1

You can use IF EXIST to check if a file exists. Adapted from this answer:

if exist "%ANT_HOME%" (
    rem file exists
) else (
    rem file doesn't exist
)
Community
  • 1
  • 1
sashoalm
  • 75,001
  • 122
  • 434
  • 781
1

you have an error in your script:

SET path=%path%;%ANT_BIN%;%ANT_BIN%;

this should be:

SET "path=%path%;%ANT_HOME%;%ANT_BIN%"

if you want to check for a not existing path you should place a backslash at the end. Otherwise this is also true, if a file with this name exists:

if exist "%ANT_HOME%\" (echo %ANT_HOME% found.) else echo Error: %ANT_HOME% not found!
Endoro
  • 37,015
  • 8
  • 50
  • 63
1

to avoid, adding your entries multiple times to the path, you should check this.

set path | find "%ANT_HOME%;%ANT_BIN%"

will give you %errorlevel% of '0' when it is already appended,or '1' when not.

Stephan
  • 53,940
  • 10
  • 58
  • 91