Running batch file with arguments from C#
Asked Answered
H

4

12

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

Hellion answered 24/1, 2013 at 9:44 Comment(13)
bat files are scripts processed with cmd.exe. Simply try to run cmd.exe with /C key.Benign
You can add a pause command to the end of the batch file and then it will wait for you to press a key before exitingGreenlaw
I need to run it through C# application. You mean to say....process.StartInfo.FileName = @"cmd.exe /c " + MyBatchFile ??Hellion
@LyubomyrShaydariv Process uses default programs if the filename is a file and not a program.Kong
@rapsalands I guess so, because it's hard to believe that a batch file can be considered as a standalone process. Please also see Default's comment if I'm wrong.Benign
edited my post with source path changed.Hellion
@rapsalands use string MyBatchFile = @"\"C:\Program Files (x86)\MybatchFile.bat\""; (note the \" around the whole expression)Kong
@Default: Getting compilation error.Hellion
change file path to string MyBatchFile = @"C:\Program~1\MybatchFile.bat"Heinrike
@rapsalands could you be more specific? what does the compilation error say?Kong
sorry......it showing me syntax errorHellion
@rapsalands that is just as little information as "compilation error". But I saw it myself. since there's a @ before the string, you should use string MyBatchFile = @"""C:\Program Files (x86)\MybatchFile.bat""";. Next time, as a suggestion, a little more details would help.Kong
@Default: Will surely keep in mind. Thanks and sorry for being a bit lazy.Hellion
K
15

What about quoting argument?

Arguments = String.Format("\"{0}\" \"{1}\"", _sourcePath, _tempTargetPath) …
Kipp answered 24/1, 2013 at 10:10 Comment(0)
M
6

.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.

Midas answered 24/1, 2013 at 10:9 Comment(0)
H
3

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
Heinrike answered 24/1, 2013 at 9:47 Comment(4)
thanks.....getting error as Files not found - Program. Edited my post with sourcepath changed.Hellion
That may be due to the paths - are there any files in the source folder (and you do need *.* for the xcopy command) - I have it running here.Heinrike
I have files in Source path. Running batch file directly does work fine. Just now noticed......when source path has any spaces....I am getting errorHellion
paths with spaces will need to be wrapped in "'s or use the DOS 8 character path.Heinrike
R
0

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 &.

Reindeer answered 19/6, 2023 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.