Richard Stingley
Richard Stingley

Reputation: 13

My batch script is reporting an error stating "was unexpected at this time"

The Windows batch file I am trying to run contains the following assignment:

set ANT_HOME="C:/Program Files/apache-ant-1.8.4"

The offending line is:

call %ANT_HOME%/bin/ant -f ../config/common.xml start_db

And when I run the script with echo on I get:

call "C:/Program_Files/apache-ant-1.8.4"/bin/ant -f ../config/common.xml start_db
Files/apache-ant-1.8.4""=="" was unexpected at this time.

I've moved the second quote to to the end of the path, after the ant, but I receive the same error message.

Upvotes: 1

Views: 9574

Answers (2)

dbenham
dbenham

Reputation: 130819

If ant were an .exe I would say your code should work. But I suspect that ant is a batch file, and the error is occurring within the ant script.

I base my conclusion on your error message - specifically the following portion of it: ""=="". The error message is a batch parsing error, and I don't see how your code could generate those characters. So I figure ant must be a batch script that is causing the problem.

I suspect ant.bat has @echo off at the top, so you are not seeing the actual line that is failing.

Not having access to the ant.bat script, I couldn't possibly diagnose exactly what is failing, nor can I guess on how to fix it.

Update - exact problem found

I found a copy of ant.bat online.

It has the following line of code within:

if "%ANT_HOME%"=="" set ANT_HOME=%DEFAULT_ANT_HOME%

Your definition of ANT_HOME includes enclosing quotes, so the code is trying to execute

if ""C:/Program Files/apache-ant-1.8.4""=="" set ANT_HOME=%DEFAULT_ANT_HOME%

The space is not quoted, and you have your error.

All you need to do to fix everything is to remove the quotes from the definition of ANT_HOME, and then add quotes to your CALL statement:

set "ANT_HOME=C:/Program Files/apache-ant-1.8.4"
call "%ANT_HOME%/bin/ant" -f ../config/common.xml start_db

Forward-slashes are not always reliable as folder delimiters within Windows. See Why does the cmd.exe shell on Windows fail with paths using a forward-slash ('/'') path separator?.

Better to use back-slashes.

set "ANT_HOME=C:\Program Files\apache-ant-1.8.4"
call "%ANT_HOME%\bin\ant" -f ..\config\common.xml start_db

Upvotes: 5

paddy
paddy

Reputation: 63451

The quotes have to completely surround a file name. You can't use them for partial names. Try this instead:

set ANT_HOME=C:\Program Files\apache-ant-1.8.4
call "%ANT_HOME%\bin\ant" -f ../config/common.xml start_db

Oh, and I changed some of your slashes to backslashes (DOS doesn't like forward slashes). I assume you are allowed to use / in that parameter you are passing though.

Upvotes: 2

Related Questions