1

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.

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
  • I think you have an unwanted underscore in your example output - `Program_Files` should read `Program Files`. – dbenham Oct 01 '12 at 03:51

2 Answers2

5

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
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Also, sorry about the extra underscore in the output. That was from a debug attempt. I reverted the forward slashes back to the original back slashes and with the quotes in the correct location, encompassing the variable name, the script worked like a champ. I am so impressed with quick and knowledgeable response provided to my question. Thanks again. – Richard Stingley Oct 02 '12 at 01:34
2

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.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • Edited to comment on usage of backslash versus ordinary slash. – paddy Oct 01 '12 at 01:58
  • 1
    Wrong on both counts - You can use quotes around partial names, even multiple times within the same path. The quotes protect spaces and special characters, and then are discarded. Give it a try :-) Also, forward slashes often work, though it seems to be bugged. CALL seems to support forward slash based on a few quick tests I just ran. See [Why does the cmd.exe shell on Windows fail with paths using a forward-slash path separator?](http://stackoverflow.com/a/10526678/1012053) for examples that don't involve CALL that work and fail. – dbenham Oct 01 '12 at 02:24
  • Your suggested edits fix the problem, but not for the reasons you state. See [my answer](http://stackoverflow.com/a/12666896/1012053). – dbenham Oct 01 '12 at 12:18
  • Glad you fixed it. For the record, in most batch files I've seen the empty-test is done like this: `if %ANT_HOME%.==.` – paddy Oct 01 '12 at 20:45
  • Actually, the safest test is `if not defined ANT_HOME`. But probably should not modify the 3rd party ant.bat file. – dbenham Oct 01 '12 at 21:54