Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running batch file with arguments from C#

I have a batch file like this

@echo off
xcopy /e %1 %2

I have my C# code as follows:

string MyBatchFile = @"C:\Program Files (x86)\MybatchFile.bat";
string _sourcePath = @"C:\FolderToCopy";
string _tempTargetPath = @"C:\TargetFolder\";

var process = new Process { 
                   StartInfo = { 
                      Arguments = string.Format("{0} {1}",
                                                _sourcePath,
                                                _tempTargetPath) 
                                } 
                          };
process.StartInfo.FileName = MyBatchFile;
bool b = process.Start();

I expect this to copy the source files to target location. But nothing happens. My console window also does not stay for enough time so that I can see the error. Can anyone guide to achieve this. I am new in batch files processing.

Edit

By adding a pause in the end of batch file. Able to reproduce error. Getting error as

Files not found - Program

Running batch file directly does work fine. Just now noticed......when source path has any spaces....I am getting error

like image 442
Sandy Avatar asked Jan 24 '13 09:01

Sandy


People also ask

How do I run a batch file from an argument in C#?

string MyBatchFile = @"C:\Program Files (x86)\MybatchFile. bat"; string _sourcePath = @"C:\FolderToCopy"; string _tempTargetPath = @"C:\TargetFolder\"; var process = new Process { StartInfo = { Arguments = string. Format("{0} {1}", _sourcePath, _tempTargetPath) } }; process.

How do I run an argument from a batch file?

In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.

How do you run a batch file in C?

Just run system("omanam. bat"); . @niko, bad command? Don't know why this would happen, but you can also try "cmd /c omanam.

Can a batch file take arguments?

Batch scripts support the concept of command line arguments wherein arguments can be passed to the batch file when invoked. The arguments can be called from the batch files through the variables %1, %2, %3, and so on.


3 Answers

What about quoting argument?

Arguments = String.Format("\"{0}\" \"{1}\"", _sourcePath, _tempTargetPath) …
like image 121
Viacheslav Ivanov Avatar answered Oct 12 '22 01:10

Viacheslav Ivanov


try

string MyBatchFile = @"C:\MybatchFile.bat";
string _sourcePath = @"C:\FolderToCopy\*.*";
string _tempTargetPath = @"C:\TargetFolder\";

i.e. add *.* to the source path

and add a 3rd line pause to the batch file

@echo off
copy /e %1 %2
pause
like image 45
qujck Avatar answered Oct 12 '22 01:10

qujck


.bat file is a text file, in order to execute it, you should start cmd process. Start it like this:

System.Diagnostics.Process.Start("cmd.exe", "/c yourbatch.bat");

Additional arguments may follow. Try this without c#, in a cmd window, or Run dialog.

like image 40
Vladimir Perevalov Avatar answered Oct 12 '22 01:10

Vladimir Perevalov