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 ?

4

3 回答 3

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
)
于 2013-05-30T06:11:19.003 回答
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!
于 2013-05-30T06:24:37.060 回答
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.

于 2013-05-30T06:31:49.063 回答