使用cmd shell合并目录的内容
我想将一个目录中的所有文件合并为一个。但是我尝试了几个版本,但似乎都不起作用。我收到一条错误消息,指出未找到该文件。这是我正在尝试的:
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);
我想要获得的示例: 复制 a.txt+b.txt+c.txt+d.txt output.txt
我得到的错误是:
System.dll 中发生类型为“System.ComponentModel.Win32Exception”的未处理异常
附加信息:系统找不到指定的文件
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试启动 cmd 而不是“启动”进程。
“copy”是命令提示符中的命令,别名为...某物,而不是 Windows 知道如何运行的实际文件本身(在命令提示符之外)。
如果您不希望在程序运行时 shell 弹出窗口,您可以使用 Process 类的一些属性来禁止该窗口出现在屏幕上。
Try starting cmd rather than "start" with process.
'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.
您是否应该使用
command
而不是files
作为Process.Start
的第二个参数?更新:
好的,所以这是一个错字。你的 inputFolder 文本怎么样?它是否对目录使用双反斜杠(转义反斜杠)?所有
\
字符都应为\\
。Should you not be using
command
instead offiles
for your second parameter toProcess.Start
?UPDATE:
Ok, so it was a typo. How about your inputFolder text? Is it using double back-slashes for the directories (escaping the back-slashes)? As in all
\
characters should be\\
.您需要使用复制命令和您的参数调用 cmd.exe (正如 @Servy 提到的)。这是代码的清理版本,可以执行您需要的操作:
您需要处理 Process(因此使用 using 语句),并且由于您要连接大量字符串(无论如何都可能有很多字符串),因此您应该使用字符串生成器。
You need to call cmd.exe with the copy command and your arguments (as was mentioned by @Servy). Here is a cleaned up version of your code to do what you need:
You need to dispose of the Process (thus the using statement) and since you are concatenating a lot of strings (potentially a lot of strings anyway), you should use a StringBuilder.