我有一个JavaScript代码,我需要进入控制台应用程序。该脚本通过cmd工作得很好但我想用它制作一个控制台应用程序,因此它更加用户友好。有人可以向我解释如何在控制台应用程序中编写此代码或使用链接将其附加到控制台应用程序中。我是控制台应用程序的新手,所以如果我问任何愚蠢的事情我会道歉: - )
当我通过cmd使用它时,我会执行以下操作; - 运行cmd。 - 键入" cd downloads"然后按Enter键。 - 输入" cscript / nologo process.js log.txt 100 200"然后按Enter键。 - 然后我将在cmd窗口中获得一个列表,我需要在下载文件夹中包含process.js和log.txt才能使其正常工作。
if(WScript.Arguments.Count() < 3)
{
WScript.Echo("Usage: cscript process.js <filename> <lower_value> <upper_value>");
WScript.Quit();
}
var filename = WScript.Arguments.Item(0);
var lowerBound = parseInt(WScript.Arguments.Item(1));
var upperBound = parseInt(WScript.Arguments.Item(2));
WScript.Echo("Here is the data from the file associated with the text 'verdi', where the");
WScript.Echo("number following 'verdi' is above " + lowerBound + " and below " + upperBound);
var fso = new ActiveXObject("Scripting.FileSystemObject")
var file = fso.OpenTextFile("log.txt", 1, false);
var lines = file.ReadAll().split('\r');
var failed = 0;
for(var idx in lines)
{
try
{
if(lines[idx].indexOf('verdi') > 0)
{
var tmp = lines[idx];
var regex = /verdi\s*\=\s*(\d+)/;
var result = regex.exec(tmp);
var num = parseInt(result[1]);
if(num >= lowerBound && num <= upperBound)
{
WScript.Echo(num);
}
}
}
catch(ex)
{
failed++;
}
}
if(failed > 0)
{
WScript.Echo("WARNING: one or more lines could not be processed!");
}
&#13;
我已经在控制台应用程序中创建了这个代码,但是它可以正常工作。我可以选择值并让cmd运行。但是我没有在窗口中获得结果并将结果打印到文档中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
private static object cmd;
private static int verdi;
private static int s;
private static int d;
public static object WScript { get; private set; }
static void Main(string[] args)
{
//Choose lower and upper value
Console.WriteLine("Choose a lower and upper value:");
string value = Console.ReadLine();
//Choose file
Console.WriteLine("Choose a file to scan:");
string file = Console.ReadLine();
//Run the javascript code
Console.WriteLine("cd downloads");
Console.WriteLine("cscript /nologo process.js {0} {1} > mydata.txt", file, value);
string command = Console.ReadLine();
Console.WriteLine("Press any key to start scan");
System.Diagnostics.Process.Start("cmd.exe", "/C" + command);
//Quit Console Application
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
}
}
&#13;
答案 0 :(得分:0)
Console.WriteLine
仅打印字符串。它不允许您执行命令。
你可以试试这个:
string command = $"cscript /nologo c:/downloads/process.js c:/downloads/{file} {lowerValue} {upperValue} > mydata.txt");
System.Diagnostics.Process.Start($"cmd.exe /C {command}");
process.js
中也有错误。该脚本始终从log.txt
读取并忽略文件名。
但为什么你在这里使用两个程序?您可以将所有代码放在一个文件中。为什么使用JavaScript作为一个而C#用于另一个?