Passing file paths to a batch file
-
21 สิงหาคม 2555 14:02
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:
String batch = @"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat"; String src = copyFrom; String dst = copyTo; 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();
And the output is:
So its not actually copying the folder, its just printing the paths its copying from and to.
Thanks
ตอบทั้งหมด
-
21 สิงหาคม 2555 16:32
The echo commands prints the strings passed into it. It's designed so that they don't execute. Does it work if you don't use echo?
- เสนอเป็นคำตอบโดย Mike FengMicrosoft, Moderator 22 สิงหาคม 2555 13:42
- ยกเลิกการนำเสนอเป็นคำตอบโดย ELSheepO 23 สิงหาคม 2555 10:50
-
21 สิงหาคม 2555 16:36
Hello, FileName should be .bat file. Try following code.
String src = copyFrom; String dst = copyTo; Process process = new Process(); process.StartInfo.WorkingDirectory = @"C:\Documents and Settings\cmolloy\My Documents\Test"; process.StartInfo.FileName = "XCOPY.bat"; process.StartInfo.Arguments = src +" "+dst; process.Start();
HTH
please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.
- แก้ไขโดย Sachin P 21 สิงหาคม 2555 16:39
-
22 สิงหาคม 2555 7:44
no I get a invalid number of parameters error.
I'm going to try what Sachyn P posted below.
-
22 สิงหาคม 2555 7:49
Hi,
That just starts a loop of the console printing, "XCOPY C:\Documets C:\Documents" and thankfully doesn't copy the contents of documents to documents, otherwise I'd have a bit of a mess on my hands :P
-
22 สิงหาคม 2555 8:21
Hello, There might be issue in using XCopy Command. I think if you use XCOPY command outside of C# code. like going to command prompt and typing Xcopy with parameters, you might see same behavior.
please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.
-
22 สิงหาคม 2555 9:02
If I just put XCOPY in the cmd window, with the two folder paths in quotes it copies all the files correctly.
-
22 สิงหาคม 2555 9:03No, just just gives me the same but without the XCOPY.bat path.
-
22 สิงหาคม 2555 9:26
Hello, Can you change the name of .bat file to XCOPY1.bat and try.
HTH
please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.
-
22 สิงหาคม 2555 9:41
The bits in the red box, what are they about? Its printing out d------- and the date, what could be causing that?
Edit
I got rid of the /k in my code and its still printing out the d----- and the date.
- แก้ไขโดย ELSheepO 22 สิงหาคม 2555 9:42
-
22 สิงหาคม 2555 10:07
Hello, don't know the reason why path is coming like that .
but you can try to enclose the arguments in double quotes , following way
process.StartInfo.Arguments = "\"" + src + "\"" + " " + "\"" + dst + "\"";
Hope it helps
please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.
-
22 สิงหาคม 2555 10:10
Still get the same, but without the C:\k
- แก้ไขโดย ELSheepO 22 สิงหาคม 2555 10:11
-
22 สิงหาคม 2555 10:12
may be some issue with path.. is it possible to try with smaller path and see if the process works. like file is copied from one folder to another.
Thanks
please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.
-
22 สิงหาคม 2555 10:31Have tried that, get the same thing
-
22 สิงหาคม 2555 13:41ผู้ดูแล
Hi ElsheepO,
Welcome to the MSDN Forum.
I don't know why Sachyn's code is failed on your side.
And I try to correct your original code: Just remove the "echo " and keep the other unchanged as Jared said. It works for me.
So would you like to try this way?
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
22 สิงหาคม 2555 14:10
string copyFrom = TestDir + "\\" + testfolder; string copyTo = @"C:\Documents and Settings\cmolloy\My Documents\ISODUMP"; //String batch = @"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat"; String src = copyFrom; String dst = copyTo; String batchCmd = String.Format("\"{0}\" \"{1}\"", src, dst); Process process = new Process(); process.StartInfo.WorkingDirectory = @"C:\Documents and Settings\cmolloy\My Documents\Test"; process.StartInfo.FileName = "XCOPY1.bat"; process.StartInfo.Arguments = String.Format("\" {0}\"", batchCmd); process.Start();Using that code I get that error, It seems to be printing out the location of the .bat file first and then the other two paths.
Why is it printing out the .bat location?
And using the line Sachyn provided the code is:
String src = copyFrom; String dst = copyTo; String batchCmd = String.Format("\"{0}\" \"{1}\"", src, dst); Process process = new Process(); process.StartInfo.WorkingDirectory = @"C:\Documents and Settings\cmolloy\My Documents\Test"; process.StartInfo.FileName = "XCOPY1.bat"; process.StartInfo.Arguments = "\"" + src + "\"" + " " + "\"" + dst + "\""; process.Start();the output is this:
- แก้ไขโดย ELSheepO 22 สิงหาคม 2555 14:13
-
22 สิงหาคม 2555 15:33ผู้ดูแล
Hi Elsheep,
This is your original code:
String batch = @"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat"; String src = copyFrom; String dst = copyTo; 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();
Please try my above suggestion first.
Thank you very very much.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
22 สิงหาคม 2555 16:13
I get this error with out the echo:
- แก้ไขโดย ELSheepO 22 สิงหาคม 2555 16:13
-
23 สิงหาคม 2555 3:08ผู้ดูแล
Hi Elsheepo,
What code does lead to this result?
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
23 สิงหาคม 2555 7:39
string copyFrom = TestDir + "\\" + testfolder; string copyTo = @"C:\Documents and Settings\cmolloy\My Documents\ISODUMP"; Process process = new Process(); String batch = @"C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY1.bat"; String src = copyFrom; String dst = copyTo; 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();Its the same code just without the echo -
24 สิงหาคม 2555 7:33ผู้ดูแล
Hi ElsheepO,
Please note this:
In your code, the batch file name is "
C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY1.bat
"
But in your above screen shot, it said:
C:\Documents and Settings\cmolloy\My Documents\Test\XCOPY.bat
So did you change something?
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- แก้ไขโดย Mike FengMicrosoft, Moderator 24 สิงหาคม 2555 7:34 typo
-
24 สิงหาคม 2555 7:45
Hello, Can you change the name of .bat file to XCOPY1.bat and try.
HTH
please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.
I changed the name here, to check if the app was actually starting the .bat file, or it was just passing XCOPY to the app.
I must not have saved the code after I changed it, so that would explain that. Sorry
This is what i get with no echo, Heres the code I used to get that.
string copyFrom = TestDir + "\\" + testfolder; string copyTo = @"C:\Documents and Settings\cmolloy\My Documents\ISODUMP"; Process p = new Process(); p.StartInfo.WorkingDirectory = @"C:\Documents and Settings\cmolloy\My Documents\Test"; p.StartInfo.FileName = "XCOPY1.bat"; p.StartInfo.Arguments = "\"" + copyFrom + "\"" + " " + "\"" + copyTo + "\""; p.Start();
- แก้ไขโดย ELSheepO 24 สิงหาคม 2555 8:06
-
24 สิงหาคม 2555 9:00ผู้ดูแล
Hi Elsheep,
Would you like to try just a const string?
I mean that don't use
string copyFrom = TestDir + "\\" + testfolder;
Try this: string copyFrom = @"directory path here";
And change this
p.StartInfo.Arguments = "\"" + copyFrom + "\"" + " " + "\"" + copyTo + "\"";
to this
p.StartInfo.Arguments = " \"" + copyFrom + "\"" + " " + "\"" + copyTo + "\"";
I added a additional space in the beginning of the arguments.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
24 สิงหาคม 2555 9:50
I can't use a constant string for copyFrom as its a variable passed down from a method finding the newest folder.
I tried changing the code
so it looks like its just printing out the XCOPY path?
-
27 สิงหาคม 2555 7:20ผู้ดูแล
Hi Elsheep,
Obviously not, it doesn't just printing out the xcopy path. You can see there is an error message "Invalid number of parameters". It indicates that the xcopy executable has been executed.
Now, you need to path the path in a double quotes. You can see the path parameters are not in a quote.
And now, I think I can tell the reason about above picture that you get a time string such as "29/06/2012" in your path parameter is you didn't format the time correct. About how to format string: http://msdn.microsoft.com/en-us/library/system.string.format.aspx
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- ทำเครื่องหมายเป็นคำตอบโดย Mike FengMicrosoft, Moderator 6 กันยายน 2555 3:42