whytheq
whytheq

Reputation: 35557

Batch not running from Console

I have a batch file foo1.bat containing this:

@echo off
  echo.>"\\xxx\xxx$\xxx\xxxx\xxxx\BATScipt Files\dblank.txt"

If I double-click the file then dblank.txt gets created as expected.

In my Console App I've tried two different approaches:

1.

System.Diagnostics.Process.Start(@"\\xxx\xxx$\xxx\xxxx\xxxx\BATScipt Files\foo1.bat");

2.

ProcessStartInfo ProcessInfo;
Process process;
ProcessInfo = new ProcessStartInfo("cmd.exe","/c \\xxx\xxx$\xxx\xxxx\xxxx\BATScipt Files\foo1.bat");
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = false;
ProcessInfo.WorkingDirectory = aPath;
process = Process.Start(ProcessInfo);
process.WaitForExit();
process.Close();

Neither of the above throws an exception but neither creates the expected output file:

If I change the code inside foo1.bat to something like the following and create a corresponding empty file bar.txt then it runs ok via the console and the text is written to bar.txt as expected:

@echo off
setlocal enableextensions
(
    systeminfo | findstr .... 
    echo %a%
    echo %b%
    echo %c%
) > "%~dp0\bar.txt"

Upvotes: 2

Views: 125

Answers (2)

Torqane
Torqane

Reputation: 146

It looks to me like you need some more double-quotes in this line:

ProcessInfo = new ProcessStartInfo("cmd.exe","/c \\xxx\xxx$\xxx\xxxx\xxxx\BATScipt Files\foo1.bat");

Your second parameter is quoted, which makes sense, but you're starting a new process with cmd /c and then feeding it \\xxx\xxx$\xxx\xxxx\xxxx\BATScipt Files\foo1.bat which contains a space.

That means you're really telling cmd to execute \\xxx\xxx$\xxx\xxxx\xxxx\BATScipt with an argument of Files\foo1.bat.

So you're going to have to add quotes around that part of the argument as well. You can build up the string or just put pairs of double quotes, or as you did in your first option, use the @ sign. I would probably just do this:

ProcessInfo = new ProcessStartInfo("cmd.exe","/c ""\\xxx\xxx$\xxx\xxxx\xxxx\BATScipt Files\foo1.bat""");

where "" is an embedded " in a double-quoted string.

Upvotes: 2

Alan
Alan

Reputation: 185

This code work for me:

Process p = new Process();
p.StartInfo.FileName = "C:\\execute.bat";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();

check shared folder permissions

this works too p.StartInfo.FileName = "\\\\xxxx\\xxx\\execute.bat";

Upvotes: 2

Related Questions