tags:

views:

2124

answers:

2

I have a bat file which sets some environment variables, and then executes a command on the command line. I want to replace the hard coded command with one passed in via a parameter.

So:

:: Set up the required environment
SET some_var=a
SET another_var=b
CALL some.bat

:: Now call the command passed into this batch file
%1

The problem is that the command is complex, and doesn't escape cleanly. It looks like this:

an.exe -p="path with spaces" -t="some text" -f="another path with spaces"

I'm trying to call the .bat from a .NET framework app, using:

Dim cmd as String = "the cmd"
System.Diagnostics.Process.Start( thebat.exe, cmd )

but I can't seem to get the escapes to work correctly. Can someone tell me how the string cmd should be entetered to get the command passed into the bat file as an argument correctly?

A: 

Why not start up the Process with the ProcessStartInfo class?

It has an EnvironmentVariables property where you could set some_var, another_var, etc. progromatticaly before running.

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "an.exe";
psi.Arguments = "-p=\"path with spaces\" -t=\"some text\" -f=\"another path with spaces\"";
psi.EnvironmentVariables["some_var"] = "a";
...
Process.Start(psi);
Clinton
After setting those environment variables, the bat I'm calling calls another bat which does additional environmental setup, so calling it via the batch file is the easiest way to ensure everything gets set up properly.
A: 

The answer may be too late, but can be useful for someone else.

I would call the batch with only one parameter - path to params.txt which contains all other parameters:

start /min cmd /c "thebat.bat C:\My Documents\params.txt"

Then read them all in the batch:

for /f "tokens=1,*" %%A in ('type "%*"') do set P%%A=%%B
echo Options:
echo P1=%P1%
echo P2=%P2%
echo P3=%P3%

params.txt:

1 "path with spaces"
2 "some text"
3 "another path with spaces"
Max