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
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.
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.
Just run system("omanam. bat"); . @niko, bad command? Don't know why this would happen, but you can also try "cmd /c omanam.
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.
What about quoting argument?
Arguments = String.Format("\"{0}\" \"{1}\"", _sourcePath, _tempTargetPath) …
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
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With