批处理文件中的桌面运行时参数?

时间:2013-04-22 21:47:00

标签: batch-file

我需要将传递输入参数传递给控制台应用程序,并使用批处理文件检索输出参数。

步骤如下:

  1. 使用输入参数运行控制台应用程序。 (这是程序当前运行的方式。)

  2. 我希望在继续下一步之前等待控制台应用程序的响应。程序需要等待,因为它正在调用Web服务。我需要确定响应代码调用web服务= 0。

  3. 下一步取决于来自Web服务的响应。

    一个。如果来自Web服务的响应= 0,则再次使用参数调用控制台应用程序以关闭Web服务上的客户帐户。

    同样在此步骤下,第二次调用控制台应用程序的结果应显示在用户可以看到的消息中。

    湾如果来自Web服务的响应不是= 0,则应向用户显示一条消息,说明问题所在。没有进一步的电话。

  4. 那么你能告诉我代码和/或指出我如何实现这个目标吗?

2 个答案:

答案 0 :(得分:1)

让我们再试一次。 这是批量代码,应该做我想要的

@echo off
call myApp.exe %*
set result=%errorlevel%
echo result = %result%

if %result%==0 call myApp.exe close (or whatever the paramaters are)
if %result%==1 echo "Message explaining what error code 1 means"
if %result%==2 echo "Message explaining what error code 2 means"
...

这将使用您传递到批处理文件的任何参数调用myApp.exe。如果需要,您可以用硬编码输入替换%*。然后打印应用程序的退出代码,并再次调用它来关闭或打印解释错误代码的消息。


MSDN修改

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "externalExecutable.exe";
p.Arguments = "-arg1 -arg2";
p.Start();
// Wait for the child process to exit before
// reading to the end of its redirected stream.
p.WaitForExit();
// Read the output stream
string output = p.StandardOutput.ReadToEnd();
// Parse the output to see what you should do
// Or just use the exit code from the proccess
if (output.Equals("0") || p.ExitCode == 0) {
   // Do what you need to in this case
} else {
   // Do something else here
}

答案 1 :(得分:0)

我在vbnet中完成了这个通用功能,真的不知道这会不会帮助你,因为我们不知道你在说什么语言。

使用此函数,您可以等待并读取进程输出(例如,进程CMD):

#Region " Run Process Function "

    ' [ Run Process Function ]
    '
    ' // By Elektro H@cker
    '
    ' Examples :
    '
    ' MsgBox(Run_Process("Process.exe")) 
    ' MsgBox(Run_Process("Process.exe", "Arguments"))
    ' MsgBox(Run_Process("CMD.exe", "/C Dir /B", True))
    ' MsgBox(Run_Process("CMD.exe", "/C @Echo OFF & For /L %X in (0,1,50000) Do (Echo %X)", False, False))
    ' MsgBox(Run_Process("CMD.exe", "/C Dir /B /S %SYSTEMDRIVE%\*", , False, 500))
    '  If Run_Process("CMD.exe", "/C Dir /B", True).Contains("File.txt") Then MsgBox("File found")

    Private Function Run_Process(ByVal Process_Name As String, Optional Process_Arguments As String = Nothing, Optional Read_Output As Boolean = False, Optional Process_Hide As Boolean = False, Optional Process_TimeOut As Integer = 999999999)

        ' Returns True if "Read_Output" argument is False and Process was finished OK
        ' Returns False if ExitCode is not "0"
        ' Returns Nothing if process can't be found or can't be started
        ' Returns "ErrorOutput" or "StandardOutput" (In that priority) if Read_Output argument is set to True.

        Try

            Dim My_Process As New Process()
            Dim My_Process_Info As New ProcessStartInfo()

            My_Process_Info.FileName = Process_Name ' Process filename
            My_Process_Info.Arguments = Process_Arguments ' Process arguments
            My_Process_Info.CreateNoWindow = Process_Hide ' Show or hide the process Window
            My_Process_Info.UseShellExecute = False ' Don't use system shell to execute the process
            My_Process_Info.RedirectStandardOutput = Read_Output '  Redirect (1) Output
            My_Process_Info.RedirectStandardError = Read_Output ' Redirect non (1) Output
            My_Process.EnableRaisingEvents = True ' Raise events
            My_Process.StartInfo = My_Process_Info
            My_Process.Start() ' Run the process NOW

            My_Process.WaitForExit(Process_TimeOut) ' Wait X ms to kill the process (Default value is 999999999 ms which is 277 Hours)

            Dim ERRORLEVEL = My_Process.ExitCode ' Stores the ExitCode of the process
            If Not ERRORLEVEL = 0 Then Return False ' Returns the Exitcode if is not 0

            If Read_Output = True Then
                Dim Process_ErrorOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Error Output (If any)
                Dim Process_StandardOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Standard Output (If any)
                ' Return output by priority
                If Process_ErrorOutput IsNot Nothing Then Return Process_ErrorOutput ' Returns the ErrorOutput (if any)
                If Process_StandardOutput IsNot Nothing Then Return Process_StandardOutput ' Returns the StandardOutput (if any)
            End If

        Catch ex As Exception
            'MsgBox(ex.Message)
            Return Nothing ' Returns nothing if the process can't be found or started.
        End Try

        Return True ' Returns True if Read_Output argument is set to False and the process finished without errors.

    End Function

#End Region