1

The following command works when entered in cmd :

"C:\Program Files\Scripts_Talend\SynchroExo_run.bat" --context_param FileCode=ABCD --context_param FilePath="//networkname/Folder/file.xls" --context_param ReportFilePath=""

But it doesn't work when I execute it with ProcessStartInfo :

ProcessStartInfo lTalendScriptInfo = new ProcessStartInfo("cmd.exe", "/c " + lCommand)
    {
        CreateNoWindow = true,
        UseShellExecute = false,
        RedirectStandardError = true,
        RedirectStandardOutput = true
    };
Process lTalendScriptProcess = Process.Start(lTalendScriptInfo);

lCommand being the same command string as shown just before. Quotes are correctly escaped thanks to \.

With ProcessStartInfo, the error is : 'C:\Program' is not recognized as an internal or external command, operable program or batch file As if the double quote was ignored and the part following the first space was as argument.

Are there any differences of interpretation between cmd and ProcessStartInfo ?

thi
  • 71
  • 1
  • 9
  • 1
    Quotes aren't ignored. They simply aren't there. Your string doesn't *contain* any quotes. The call is wrong anyway - you *don't* need to call `cmd.exe` to run any executable, even if it's a batch file. Just use `new ProcessStartInfo("C:\Program Files\Scripts_Talend\SynchroExo_run.bat", "--context_param FileCode=ABCD --context_param FilePath=`"....."` – Panagiotis Kanavos Feb 23 '18 at 10:53

2 Answers2

2

Your string doesn't contain any quotes. It's just :

C:\Program Files\Scripts_Talend\SynchroExo_run.bat

When you concatenate that with the arguments you end up with a string that can't be executed.

In any case, you don't need to call cmd.exe to run any executable or batch, just pass the path as the argument to ... the executable. There's no need to use extra strings here because the parameter expects a path:

var batchPath="C:\Program Files\Scripts_Talend\SynchroExo_run.bat";
var arguments = "--context_param FileCode=ABCD --context_param FilePath=\"//networkname/Folder/file.xls\" --context_param ReportFilePath=\"\"";

var procInfo= new ProcessStartInfo(batchPath, arguments)
{
    CreateNoWindow = true,
    UseShellExecute = false,
    RedirectStandardError = true,
    RedirectStandardOutput = true
};
var scriptProcess= Process.Start(procInfo);
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
1

Wrapping the entire command string with a pair of \" made it work.

The final command string looks like that :

""C:\Program Files\Scripts_Talend\SynchroExo_run.bat" --context_param FileCode=ABCD --context_param FilePath="//networkname/Folder/file.xls" --context_param ReportFilePath="""
thi
  • 71
  • 1
  • 9
  • 1
    You simply covered up the problem. You don't need to use `cmd.exe` in the first place, just call the batch. Double quotes need *escaping* too. You missed the double quote in the arguments – Panagiotis Kanavos Feb 23 '18 at 10:56