Reputation: 11697
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
Upvotes: 12
Views: 43252
Reputation: 11835
The reason the batch file fails with many of the c# suggestions is that at run time the internal variables are themselves UNQUOTED.
Simply in addition to the C# passed parameters need "double quoting" so do the internal ones thus:
xcopy /e %1 %2
needs replacing with
xcopy /e "%~1" "%~2"
so as to convey the external "
to be used internally thus ensuring "c:\program files\...\..."
is unbroken when used by the internal command.
Upvotes: 0
Reputation: 517
You can declare a proc variable of type System.Diagnostic.Process and then assign the bat name and parameters to it. For example, xxcopy.bat copies the name of the pdf you send to another address.
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "D:\\xxcopy.bat";
proc.StartInfo.Arguments = String.Format("{0}", "MiFile3.pdf");
proc.Start();
Upvotes: 0
Reputation: 3724
I have found that using this approach or calling cmd.exe
is problematic with long argument lists. For those cases, I have found that using powershell.exe
instead of cmd.exe
to work well:
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}' '{2}'\"",
MyBatchFile,
_sourcePath,
_tempTargetPath)
}
};
process.StartInfo.FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
bool b = process.Start();
Note that the entire arguments list needs to be wrapped in quotes and the batch file path is preceeded by an &
.
Upvotes: -1
Reputation: 1555
What about quoting argument?
Arguments = String.Format("\"{0}\" \"{1}\"", _sourcePath, _tempTargetPath) …
Upvotes: 15
Reputation: 4157
.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.
Upvotes: 5
Reputation: 14578
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
Upvotes: 2