RichardMc
RichardMc

Reputation: 854

Passing arguments to batch file that have spaces in it

Given the following command in a batch file:

start /wait "" %1"Certs\makecert.exe" -n "CN="%2 -pe -ss Root -sr LocalMachine -sky exchange -m 120 -a sha1 -len 2048 -r

You can see it expects two arguments to be passed into the batch file. The first being a folder address and the second being a subject name for the certificate being created. The problem lies in the folder address where it has a spaces in the address: C:\Program Files (x86).

The code launching the batch file looks like:

private void UseBatchFile(string filePath, string parameters)
    {
        using (Process process = new Process()
        {
            StartInfo = new ProcessStartInfo()
            {
                UseShellExecute = false,
                CreateNoWindow = true,
                FileName = filePath,
                Arguments = parameters,
            }
        })
        {
            process.Start();
            process.WaitForExit();
        }
    }

The arguments passed in would look as follows:

"C:\\Program Files (x86)\\ TestCA"

The batch file will read in the C:\Program as it's first argument which resolves to

C:\ProgramCerts\makecert.exe

I have tried enclosing the string argument address in '' and using %~1 in the batch file to remove the quotes. This does not do anything either. I cannot use %1%2%3 to creat the full address as other filepaths can be enetered into this batch file.

How can I pass in the address as one argument?

EDIT: The method is being called as follows:

this.UseBatchFile(string.Format("{0}{1}", PathName, @"Certs\CACert.bat"), string.Format("{0} {1}", PathName, "TestCA"));

PathName being : C:\Program Files (x86)

Upvotes: 0

Views: 1115

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 100019

First of all, replace this:

string.Format("{0}{1}", PathName, @"Certs\CACert.bat")

with this:

Path.Combine(PathName, @"Certs\CACert.bat")

Here is the reference for how Windows is treating those command line arguments, including numerous examples:

Parsing C++ Command-Line Arguments (MSDN)

Upvotes: 1

Related Questions