Ashwin Hegde
Ashwin Hegde

Reputation: 1771

Batch Script Optimization

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 ?

Upvotes: 1

Views: 357

Answers (3)

Endoro
Endoro

Reputation: 37579

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!

Upvotes: 1

Stephan
Stephan

Reputation: 56188

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.

Upvotes: 1

sashoalm
sashoalm

Reputation: 79595

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
)

Upvotes: 1

Related Questions