Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merging the content of a directory using cmd shell

I want to merge all the files in a directory into one. However I tried several versions but none of them seem to work. I am getting an error saying that the file was not found. Here is what I was trying:

        String outputFile = this.outputTxt.Text;
        String inputFolder = this.inputTxt.Text;
        String files = "";
        String command;
        foreach (String f in Directory.GetFiles(inputFolder))
        {
            files += f+"+";
        }
        files = files.Substring(0, files.Length - 1);
        command = files + " " + outputFile;

        Process.Start("copy",command);

sample of what I want to obtain: copy a.txt+b.txt+c.txt+d.txt output.txt

And the error I get is:

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll

Additional information: The system cannot find the file specified

like image 730
Corovei Andrei Avatar asked Jan 26 '26 12:01

Corovei Andrei


1 Answers

Try starting cmd rather than "start" with process.

Process.Start("cmd", "copy " + command);

'copy' is a command in the command prompt, aliased to... something, and not an actual file itself that windows knows how to run (outside of the command prompt).

There are properties of the Process class that you can use to suppress the window that the shell pops up if you don't want it on the screen while the program is running.

like image 63
Servy Avatar answered Jan 29 '26 00:01

Servy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!