需要帮助将此代码获取到控制台应用程序

Need help to get this code to console application

本文关键字:获取 控制台 应用程序 代码 帮助      更新时间:2023-09-26

我有一个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!");
}

我已经在控制台应用程序中编写了这段代码,但它不能正常工作。我可以选择值并运行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();
                      
        }
    }
}

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#?