views:

515

answers:

2

Hello all,

I am trying to setup a nAnt task to run a batch file with 4 parameters. First 2 parameters are folder path, so it required to be wrapped with double quote. Here's my build file

<?xml version="1.0"?>
<project name="Proejct" default="dbsetup" >
  <description>nAnt script</description>

  <property name="project.SQLBinFolder" value='"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\"' />
  <property name="project.SQLDataFolder" value='"C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\"' />
  <property name="project.DBServerName" value="JJADEJA-VISTA-P\SQLEXPRESS" />
  <property name="project.DatabaseName" value="dbMain" />

  <target name="test">
    <echo message="..\Database\RunDBSQL.bat ${project.SQLBinFolder} ${project.SQLDataFolder} ${project.DBServerName} ${project.DatabaseName}" />
  </target>

  <target name="dbsetup" description="Setup database script">
    <exec program="..\Database\RunDBSQL.bat" >
      <arg value="${project.SQLBinFolder}" />
      <arg value="${project.SQLDataFolder}" />
      <arg value="${project.DBServerName}" />
      <arg value="${project.DatabaseName}" />
    </exec>
  </target>
</project>

my test target outputs

..\Database\RunDBSQL.bat "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\" "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\dbCIMA" JJADEJA-VISTA-P\SQLEXPRESS dbCIMA

which is works fine from command line.

When I run dbsetup target, it gives me following error.

dbsetup:

     [exec] Files\Microsoft was unexpected at this time.

BUILD FAILED - 0 non-fatal error(s), 1 warning(s)

c:\Projects\CIMA\Trunk\Solution file\CIMA.build(15,6):
External Program Failed: c:\Projects\CIMA\Trunk\Database\RunDBSQL.bat (return co
de was 255)

Total time: 0.1 seconds.

My RunDBSQL.bat

SET SQLBinFolder="%1"
SET SQLDataFolder="%2"
SET DBServerName=%3
SET DatabaseName=%4

SET PATH="%1%"

cls
echo.
echo *****************************
echo * Database build and update *
echo *****************************
echo.

echo SQLCmd Exe Path : %SQLBinFolder%
echo SQL Data Folder Path : %SQLDataFolder%
echo Database server: %DBServerName%
echo Database name  : %DatabaseName%
echo.

I trying wrapping my variables and property in all kind of combination of wrapping / escaping, but no luck. Anyone has got any suggestion or idea, please let me know.

Thanks In advance, Jay

A: 

I believe it will work if you encode the double quotes with &quot;

BigPigVT
tried it, no luck
Jay
+1  A: 

If you take a look at the edit history of this answer, you will see how terribly wrong my first guess was. You rather need less than more quotes. An arg node's value attribute is quoted automatically if it's a path containing whitespace.

<?xml version="1.0"?>
<project name="Proejct" default="dbsetup" >
  <description>nAnt script</description>
  <property name="project.SQLBinFolder" value="C:\Program Files\Microsoft SQL Server\90\Tools\Binn\" />
  <property name="project.SQLDataFolder" value="C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\" />
  <property name="project.DBServerName" value="JJADEJA-VISTA-P\SQLEXPRESS" />
  <property name="project.DatabaseName" value="dbMain" />
  ...
  <target name="dbsetup" description="Setup database script">
    <exec program="..\Database\RunDBSQL.bat" >
      <arg value="${project.SQLBinFolder}" />
      <arg value="${project.SQLDataFolder}" />
      <arg value="${project.DBServerName}" />
      <arg value="${project.DatabaseName}" />
    </exec>
  </target>
</project>

RunDBSQL.bat: Remove quotes here. Arguments passed by NAnt will be qouted appropriately.

SET SQLBinFolder=%1
SET SQLDataFolder=%2
SET DBServerName=%3
SET DatabaseName=%4
...

CAVEAT: NAnt's automatic quoting facility doesn't work if the argument's value is combined from a command-line switch and a path containing whitespace. Example:

<property name="bar.path" value="C:\Program Files\bar" />
<exec program="foo.bat" >
  <arg value="-f:${bar.path}" />
</exec>

This is what I had in mind when I suggested explicit quoting:

<property name="bar.path" value="C:\Program Files\bar" />
<exec program="foo.bat" >
  <arg value="-f:&quot;${bar.path}&quot;" />
</exec>
The Chairman
I tried this, but no luck. Even after trying different option I get either to script to work (but it seems to be passing parameters without quotes). or give the same error.
Jay
You're right, Jay. I updated my answer (well, it's more a complete rewrite than an update). This should work now.
The Chairman