ELSheepO
ELSheepO

Reputation: 305

Passing file paths to a batch file

I'm calling a .bat file to XCOPY a folder. Is there anyway to pass the file name and the destination into the batch file?

My .bat file

XCOPY %1 %2
pause

The code I'm using to call the .bat is this:

process.Start(@"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat");

I've tried this code

process.Start(@"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat" , copyfrom copyto);

As i've used that before for shutting down my comp, but it doesn't work with this.

Thanks

Update

process.StartInfo.FileName = @"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat";
process.StartInfo.Arguments = copyFrom.ToString();
process.StartInfo.Arguments = copyTo.ToString();
process.Start();

That is the code I'm using but It doesn't work. I'm getting this from the XCOPY screen: enter image description here

So it doesn't look like its taking the full file paths. copyto and copyfrom are variables that contain the paths.

UPDATE

Using azhrei's code:

String batch = @"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat";
String src = @"C:\Tricky File Path\Is Here\test1.txt";
String dst = @"C:\And\Goes Here\test2.txt";  
String batchCmd = String.Format("\"{0}\" \"{1}\" \"{2}\"", batch, src, dst);

process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = String.Format("/k \"echo {0}\"", batchCmd);

process.Start();

I'm getting this output: enter image description here

Which isn't actually copying the file.

Upvotes: 2

Views: 3567

Answers (4)

azhrei
azhrei

Reputation: 2323

You're starting a batch file - you'll need to use cmd.exe

Surround each argument with " (needed if the argument has spaces).

  String batch = @"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat";
  String src = @"C:\Tricky File Path\Is Here\test1.txt";
  String dst = @"C:\And\Goes Here\test2.txt";  
  String batchCmd = String.Format("\"{0}\" \"{1}\" \"{2}\"", batch, src, dst);

  process.StartInfo.FileName = "cmd.exe";
  process.StartInfo.Arguments = String.Format("/k \"{0}\"", batchCmd);

  process.Start();

If your batch file literally xcopies and nothing else, then you can just replace cmd.exe with xcopy.exe and remove the /k + batch.

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

you can use Arguments property

Process proce = new Process();
proce.StartInfo.FileName = "yourfile.exe";
proce.StartInfo.Arguments =  ..;
proce.Start();

Article : http://www.c-sharpcorner.com/UploadFile/jawedmd/xcopy-using-C-Sharp-to-copy-filesfolders/

Upvotes: 2

M Afifi
M Afifi

Reputation: 4795

Replace it with this,

var process = new Process();
process.StartInfo.FileName = @"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat";
process.StartInfo.Arguments = // my arguments
process.Start();

A better option would be to replace what your XCOPY.BAT file is doing with the equivalent calls in System.IO (you get error handling then).

Upvotes: 1

Related Questions