Skip to content

Instantly share code, notes, and snippets.

@codeartery
Last active July 21, 2024 05:32

Revisions

  1. codeartery renamed this gist Sep 5, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. codeartery revised this gist May 4, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CmdOut.vbs
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ Function CmdOut( pCmd )
    REM@returns
    ' CmdOut <string> - The output of the command.
    REM@author
    ' Jeremy England, https://codeartery.com/
    ' Jeremy England, http://codeartery.com/
    REM@mini
    ' Function CmdOut(p):Dim w,e,r,o:Set w=CreateObject("WScript.Shell"):Set e=w.Exec("Cmd.exe"):e.StdIn.WriteLine p&" 2>&1":e.StdIn.Close:While(InStr(e.StdOut.ReadLine,">"&p)=0)::Wend:Do:o=e.StdOut.ReadLine:If(e.StdOut.AtEndOfStream)Then:Exit Do:Else:r=r&o&vbLf:End If:Loop:CmdOut=r:End Function
    Dim oWss, oExe, Return, Output
  3. codeartery created this gist Dec 30, 2018.
    26 changes: 26 additions & 0 deletions CmdOut.vbs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    Function CmdOut( pCmd )
    REM@description
    ' Run a command prompt command and get its output.
    REM@params
    ' pCmd <string> - A command prompt command.
    REM@returns
    ' CmdOut <string> - The output of the command.
    REM@author
    ' Jeremy England, https://codeartery.com/
    REM@mini
    ' Function CmdOut(p):Dim w,e,r,o:Set w=CreateObject("WScript.Shell"):Set e=w.Exec("Cmd.exe"):e.StdIn.WriteLine p&" 2>&1":e.StdIn.Close:While(InStr(e.StdOut.ReadLine,">"&p)=0)::Wend:Do:o=e.StdOut.ReadLine:If(e.StdOut.AtEndOfStream)Then:Exit Do:Else:r=r&o&vbLf:End If:Loop:CmdOut=r:End Function
    Dim oWss, oExe, Return, Output
    Set oWss = CreateObject( "WScript.Shell" )
    Set oExe = oWss.Exec( "Cmd.exe" )
    Call oExe.StdIn.WriteLine( pCmd & " 2>&1" )
    Call oExe.StdIn.Close()
    While( InStr( oExe.StdOut.ReadLine, ">" & pCmd ) = 0 ) :: Wend
    Do : Output = oExe.StdOut.ReadLine()
    If( oExe.StdOut.AtEndOfStream )Then
    Exit Do
    Else
    Return = Return & Output & vbLf
    End If
    Loop
    CmdOut = Return
    End Function
    13 changes: 13 additions & 0 deletions {1} usage.vbs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    REM@usage
    ' Put the full or mini class/sub/function in your script to use.
    Function CmdOut(p):Dim w,e,r,o:Set w=CreateObject("WScript.Shell"):Set e=w.Exec("Cmd.exe"):e.StdIn.WriteLine p&" 2>&1":e.StdIn.Close:While(InStr(e.StdOut.ReadLine,">"&p)=0)::Wend:Do:o=e.StdOut.ReadLine:If(e.StdOut.AtEndOfStream)Then:Exit Do:Else:r=r&o&vbLf:End If:Loop:CmdOut=r:End Function

    ' returns the result of whatever command you run
    Dim Result
    Result = CmdOut( "ECHO Hello, world!" )
    MsgBox Result

    ' if you run with cscript instead of wscript you can see the full output when using wscript.echo because msgbox has a character limit
    Dim IpConfig
    IpConfig = CmdOut( "ipconfig /all" )
    WScript.Echo IpConfig