【问题标题】:Reading output data from Command Line using vbscript使用 vbscript 从命令行读取输出数据
【发布时间】:2016-02-12 15:38:08
【问题描述】:

如何使用 Vbscript 从命令提示符读取内容?

例如:

如果我运行date /T 命令,它将返回当前系统日期。

那么如何读取输出日期呢?

谢谢!

【问题讨论】:

    标签: windows vbscript qtp hta


    【解决方案1】:

    类似的东西:

    Msgbox RunDos("Date /T")
    '******************************************************************************************
    Function RunDos(strCommand)
    Dim wsh, fs, ts
    Dim strTempFile, strData
    Const ForReading = 1
    Const TemporaryFolder = 2
    Const WshHide = 0
        Set wsh = CreateObject("Wscript.Shell")
        Set fs = CreateObject("Scripting.FileSystemObject")
        strTempFile = fs.BuildPath(fs.GetSpecialFolder(TemporaryFolder).Path, fs.GetTempName)
        wsh.Run "cmd.exe /c " & strCommand & " > """ & strTempFile & """", WshHide, True
        Set ts = fs.OpenTextFile(strTempFile, ForReading, True)
        strData = ts.ReadAll
        RunDos = strData
        ts.Close
    End Function
    '******************************************************************************************
    

    编辑:使用输入框添加另一个代码

    Option Explicit
    Dim Mycmd
    Mycmd = InputBox("Type a command line to be executed ! "& vbcr &"For example Ping www.google.com",_
    "Type a command line to be executed !","Ping www.google.com")
    If Mycmd = "" Then Wscript.Quit
    wscript.echo Run_Cmd(Mycmd)
    '**********************************************************************************************
    Function Run_Cmd(strCommand)
        Const ForReading = 1
        Const TemporaryFolder = 2
        Const WshHide = 0
        Dim wsh, fs, ts
        Dim strTempFile,strFile, strData
        Set wsh = CreateObject("Wscript.Shell")
        Set fs = CreateObject("Scripting.FileSystemObject")
        strTempFile = fs.BuildPath(fs.GetSpecialFolder(TemporaryFolder).Path, fs.GetTempName)
        strFile = fs.BuildPath(fs.GetSpecialFolder(TemporaryFolder).Path, "result.txt")
        wsh.Run "cmd.exe /c " & strCommand & " > " & DblQuote(strTempFile) & "2>&1", WshHide, True
        wsh.Run "cmd.exe /u /c Type " & DblQuote(strTempFile) & " > " & DblQuote(strFile) & "", WshHide, True
        Set ts = fs.OpenTextFile(strFile, ForReading,true,-1)
        strData = ts.ReadAll
        Run_Cmd = strData
        ts.Close
        fs.DeleteFile strTempFile
        fs.DeleteFile strFile
    End Function
    '**********************************************************************************************
    Function DblQuote(Str)
        DblQuote = Chr(34) & Str & Chr(34)
    End Function
    '**********************************************************************************************
    

    另一个向您展示如何使用 HTA 文件执行命令行:CommandLine.hta

    <html>
    <title>Execution of command line with HTA by Hackoo</title>
    <head>
    <HTA:APPLICATION 
         APPLICATIONNAME="Execution of command line with HTA by Hackoo"
         SCROLL="no"
         SINGLEINSTANCE="yes"
         WINDOWSTATE="maximize"
         ICON="Winver.exe"
    />
    </head>
    <META HTTP-EQUIV="MSThemeCompatible" CONTENT="YES">
    <script language="VBScript">
    Option Explicit
    Dim Title : Title = "Execution of command line with HTA by Hackoo"
    '**********************************************************************************************
    Sub Window_OnLoad
        Call Run_Cmd("help")
    End Sub
    '********************************************************************************************** 
    Sub Run_Cmd(strCommand)
    On Error Resume Next
        If input.value = "" Then
            MsgBox "ATTENTION ! The text box is empty !"& vbcr &_
            "You forgot to type a command on the text box !",vbExclamation,Title
            Exit Sub
        End if
        Output.value = ""
        btnClick.disabled = True
        document.body.style.cursor = "wait"
        btnClick.style.cursor = "wait"
        Const ForReading = 1
        Const TristateTrue = -1
        Const TemporaryFolder = 2
        Const WshHide = 0
        Dim wsh, fs, ts
        Dim strTempFile,strFile, strData
        Set wsh = CreateObject("Wscript.Shell")
        Set fs = CreateObject("Scripting.FileSystemObject")
        strTempFile = fs.BuildPath(fs.GetSpecialFolder(TemporaryFolder).Path, fs.GetTempName)
        strFile = fs.BuildPath(fs.GetSpecialFolder(TemporaryFolder).Path, "result.txt")
        wsh.Run "cmd.exe /c " & strCommand & " > " & DblQuote(strTempFile) & "2>&1", WshHide, True
        wsh.Run "cmd.exe /u /c Type " & DblQuote(strTempFile) & " > " & DblQuote(strFile) & "", WshHide, True
        Set ts = fs.OpenTextFile(strFile,ForReading,True,TristateTrue)
        strData = ts.ReadAll
        Output.Value = "Microsoft Windows [version 7.1 7631]" & vbcrlf &_
        "Copyright (c) 2009 Microsoft Corporation. All rights reserved." & vbcrlf & vbcrlf &_
        "C:\>"& strCommand & vbcrlf & strData
        ts.Close
        fs.DeleteFile strTempFile
        fs.DeleteFile strFile
        document.body.style.cursor = "default"
        btnClick.style.cursor = "default"
        btnClick.disabled = False   
    End Sub
    '**********************************************************************************************
    Function DblQuote(Str)
        DblQuote = Chr(34) & Str & Chr(34)
    End Function
    '**********************************************************************************************
    Sub OnClickButtonCopy()
        document.parentwindow.clipboardData.SetData "text", Output.Value
        MsgBox "The ouput result is copied to the clipboard !",vbInformation,Title
    End Sub
    '**********************************************************************************************
    </script>
    </head>
    <body bgcolor="123456" text=darkorange>
    <hr>
    <center><FONT SIZE="3"><B><I>Some examples of commands</I></B></FONT><BR>
    <select style="background-color:lightblue" name="DropDown">
    <option value="CD %Programfiles%\Mozilla Firefox\ | Start Firefox.exe">CD %Programfiles%\Mozilla Firefox\ | Start Firefox.exe</option>
    <option value="Tracert www.google.fr">Tracert www.google.fr</option>
    <option value="Start iexplore">Start iexplore</option>
    <option value="Start Notepad">Start Notepad</option>
    <option value="Start Winword">Start Winword</option>
    <option value="Explorer.exe /n,/e,/root,C:\Program Files">Explorer.exe /n,/e,/root,C:\Program Files</option>
    <option value="Ipconfig">IpConfig</option>
    <option value="Dir">Dir</option>
    <option value="Ping www.yahoo.fr">Ping www.yahoo.fr</option>
    <option value="Ping www.google.fr">Ping www.google.fr</option>
    </select>
    <input type="button" onClick="Run_Cmd(DropDown.value)" value="Run this command">
    <center><hr><B><I>Type your input command here</I></B><br>
    <input type="text" Name="input" size="10"style="width:100%" value="Ping www.google.com" style="background-color:lightblue">
    <input type="submit" name="btnClick" value="Run the input command line" onclick="Run_Cmd(input.value)"> 
    <br><hr><B><I> The output result (readonly)</I></B><hr>
    <textarea readonly id="Output" style="width:100%" rows="28" style="background-color:black; color:white">Microsoft Windows [version 7.1 7631]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.
    C:\></textarea><input type="button" name="ButtonCopy" value="Copy the ouput result to the Clipboard" onclick="OnClickButtonCopy">
    <hr></center>
    </body>
    </html>
    

    CommandLine.hta 的屏幕截图

    【讨论】:

    • 嗨,Hackoo,您能否详细说明一下。如果我可以输入屏幕上可见的内容,请发表评论,然后我会阅读。对于前。 Systemutil.Run "cmd.exe" window("object class:=ConsoleWindowClass").Type "cd\Test Result"
    • @AlokeshBehera 检查我的最后编辑:添加另一个带有输入框的代码;还有,如何使用 HTA 文件执行命令行
    • @Hackooo 我需要帮助。你能告诉我下面这行是什么意思吗....wsh.Run "cmd.exe /c " & strCommand & " > " & DblQuote(strTempFile) & "2>&1", WshHide
    【解决方案2】:

    可以使用从%comspec% /c date /T获取的WshScriptExec对象:

    >> WScript.Echo CreateObject("WScript.Shell").Exec("%comspec% /c date /T").Stdout.ReadAll()
    >>
    10.02.2016
    

    不过很简单

    >> WScript.Echo Date()
    >>
    10.02.2016
    

    风险较小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-07
      • 2013-05-31
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      相关资源
      最近更新 更多