0

I am trying to code something that will essentially concat a bunch of files together into 1 output file.

my code is as follows

string[] destination = new string[this.lbFiles.Items.Count];
this.lbFiles.Items.CopyTo(destination, 0);
string result1 = ConvertStringArrayToString(destination);
result1 = result1.Remove(result1.Length - 3);
string outputfile = this.saveFileDialog1.FileName;
string copyarg = "copy /b " + result1 + quote + outputfile + quote;
System.Diagnostics.Process.Start("CMD.exe", copyarg);

So basically result1 = all the files i'm trying to concatenate. with full paths and quotes. and outputfile = the output file name i want to use with full path.

My problem is, when I execute the code, it copys the files, but it doesnt use the output file name I have specified, and it outputs the file to the directory where the program exe is located, not the path I have specified.

Any help?

Derek
  • 131
  • 1
  • 1
  • 7
  • 4
    Why aren't you using `File.Copy` and its friends? – TDaver Nov 08 '12 at 17:00
  • 3
    @TDaver probably because the command-line version can be used to concatenate files, while File.Copy cannot. That said, `cmd copy /b` is still the wrong way to do this. Just open an output stream and read in from each file, write back to the output. – Joel Coehoorn Nov 08 '12 at 17:06
  • 3
    But C# has it's fair share of tricks for concatting too. See: http://stackoverflow.com/questions/444309/what-would-be-the-fastest-way-to-concatenate-three-files-in-c – TDaver Nov 08 '12 at 17:09
  • @Derek - what is the value of copyarg when you call Process.Start? – Polyfun Nov 08 '12 at 17:10
  • /C copy /b "d:\test1.ac3" + "d:\test2.ac3" "d:\output.ac3" that would be the value of copyarg – Derek Nov 08 '12 at 17:42
  • I have a bunch of files in a listbox. I copy them into a string array, and then turn that string array into a string that contains "file1" + "file2" etc...i then feed that into the cmd copy process. my problem is, it's not respecting the full path – Derek Nov 08 '12 at 17:45
  • I think you're either missing a quote in there, or a space. You're removing the last three characters of `result1`, which I assume are space-plus-space, and then immediately adding a quote. This would produce `copy /b "d:\test1.ac3" + "d:\test2.ac3""d:\output.ac3"`. I have no idea what that would do if you ran it, though. – Bobson Nov 08 '12 at 18:08
  • Bobson you are a genius! that was it! i changed it to remove 2 characters instead and it works! Thanks so much! – Derek Nov 08 '12 at 18:24
  • @Derek - Glad it helped! I added it as an answer so you can accept it if you choose. Good luck with your code! – Bobson Nov 08 '12 at 18:49

1 Answers1

1

You appear to be either missing a quote in there, or a space. You're removing the last three characters of result1, which I assume are "+", and then immediately adding a quote. This would produce copy /b "d:\test1.ac3" + "d:\test2.ac3""d:\output.ac3".

I would assume this would error, but if it doesn't, it could certainly produce your odd behavior. Especially if your file names actually have spaces in them.

Bobson
  • 13,498
  • 5
  • 55
  • 80