简体   繁体   English

将更多参数传递给C#中已经打开的cmd提示符

[英]Pass more arguments to an already opened cmd prompt in C#

I want to loop through all images in a folder, retrieve each image's name and pass an argument using the name. 我想遍历文件夹中的所有图像,检索每个图像的名称并使用名称传递一个参数。 This is what I have: 这就是我所拥有的:

foreach (string imageFile in Directory.EnumerateFiles(filePath))
{
    //Get image name and save it as string
    string args = "something"; //a line of argument with image name in it
    Process.Start("cmd", @"/c cd C:\Tesseract-OCR && " + args);
}

The problem with above code is that, for each image file, it will open up a new command prompt. 上面的代码的问题在于,对于每个图像文件,它将打开一个新的命令提示符。 Instead, I want something like: 相反,我想要类似的东西:

Process.Start("cmd", @"/k cd C:\Tesseract-OCR");
foreach (string imageFile in Directory.EnumerateFiles(filePath))
{
    //For each imageFile I have, pass an argument to the opened cmd prompt
}

There are a few methods, but generally the simplest is to create a batch file with all of the commands you want to execute then pass that as a parameter to cmd . 有几种方法,但是通常最简单的方法是使用要执行的所有命令创建一个批处理文件,然后将其作为参数传递给cmd

Something like this: 像这样:

string imageFolder = @"C:\Path\To\Images";
string batchFile = @"C:\Temp\cmds.cmd";
string outputFile = @"C:\Temp\cmds.log";

// substitute your own command here.  "{0}" will be substituted for filename 
string command = @"attrib ""{0}"" >> """ + outputFile + @"""";

// delete the batch file if it exists
if (File.Exists(batchFile))
    File.Delete(batchFile);

// create batch file from content of image folder
using (var writer = File.CreateText(batchFile))
{
    writer.WriteLine("@echo off");

    foreach (var file in Directory.EnumerateFiles(imageFolder, "*.jpg"))
        writer.WriteLine(command, file);
}

// Delete the output file if it exists
if (File.Exists(outputFile))
    File.Delete(outputFile);

// Execute the batch
var p = Process.Start("cmd", @"/c """ + batchFile + @"""");
p.WaitForExit();

After that completes you can grab the content of the output file and parse it for results. 完成之后,您可以获取输出文件的内容并对其进行解析以获取结果。 If you need a break between the outputs, just echo something distinctive between each one. 如果您需要在输出之间进行中断,只需在每个输出之间回显一些独特的内容即可。 Maybe something like: 也许像这样:

// create batch file from content of image folder
using (var writer = File.CreateText(batchFile))
{
    writer.WriteLine("@echo off");

    foreach (var file in Directory.EnumerateFiles(imageFolder, "*.jpg"))
    {
        writer.WriteLine(@"echo -----Start: {0}>>""{1}""", file, outputFile);
        writer.WriteLine(command, file);
        writer.WriteLine(@"echo -----End: {0}>>""{1}""", file, outputFile);
    }
}

That way you get the filename in the output as well to help with parsing. 这样,您还可以在输出中获取文件名,以帮助解析。


Another option is to use full stream redirection to streams that you can write commands to and read responses from. 另一种选择是使用完全流重定向到可以向其写入命令和从中读取响应的流。 This would allow you to have a command prompt running somewhere in the background that you can issue commands to. 这样一来,您便可以在后台某处运行命令提示符,以向其发出命令。 Seems simple, but honestly it takes a lot of work to get just right. 看起来很简单,但老实说,要正确就需要大量的工作。 If you want to go this way I'd suggest redirecting all three standard streams. 如果您想采用这种方式,建议您重定向所有三个标准流。

为什么程序不创建包含所有要执行的文件/命令的批处理文件,然后使用Process.Start执行该批处理文件?

You can use Directory.GetFiles() to get a string array of all the files in a directory. 您可以使用Directory.GetFiles()获取目录中所有文件的字符串数组。 Then use String.Join() to merge the array into one long string of file names separated by space. 然后使用String.Join()将数组合并为一个由空格分隔的长文件名字符串。 I would also wrap each item in quotes if your path or files contain spaces. 如果您的路径或文件包含空格,我也将每个项目用引号引起来。

const string QUOTE = "\"";
const string SPACE = " ";
const string SEPARATOR = QUOTE + SPACE + QUOTE;

string[] files = Directory.GetFiles(filePath, "*.png");
string allFiles = QUOTE + String.Join(SEPARATOR, files) + QUOTE;
Console.WriteLine(@"cmd.exe /c cd C:\Tesseract-OCR && " + allFiles);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM