Running a batch file when the argument contains quotes and spaces (from .NET framework)

I have a bat file that sets some environment variables and then runs a command on the command line. I want to replace a hardcoded command with one passed through 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 team is complex and doesn't disappear cleanly. It looks like this:

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

      

I am trying to call .bat from a .NET framework application using:

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

      

but I can't seem to get the escapades to work correctly. Can anyone tell me how the cmd line should be included in order to get the command passed to the bat file as an argument correctly?

+1


a source to share


2 answers


The answer might be too late, but it might be helpful to someone else.

I would name the package with only one parameter - the path to params.txt, which contains all the other parameters:

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

      

Then read them all in the package:



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"

      

+1


a source


Why not run Process

with a class ProcessStartInfo

?

It has a property EnvironmentVariables

where you could set some_var

, another_var

etc. progromatticaly before launch.



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);

      

0


a source







All Articles