4

This is what I have so far. It works; outputing the folder path to temp to a text file. What I really want, is to output the data to a variable. Every example I see online, show how to do this using something like:

set objScriptExec = wshShell.Exec (strCommand) 

followed by

strresult = LCase(objScriptExec.StdOut.ReadAll. // code

I want this to run with Run, not Exec, because I want the command prompt windows to be hidden as I will performing many commands with the code below. How can I capture that output to a variable?

Set wsShell = CreateObject("WScript.Shell")
strCommand = "cmd /c echo %temp% > %temp%\test.txt"
wsShell.Run strcommand,0,True
Ken White
  • 123,280
  • 14
  • 225
  • 444
raylward102
  • 59
  • 1
  • 1
  • 4
  • I'm sure youve already looked into this, but why not store in a text file, read the text file, then delete it? http://stackoverflow.com/questions/5690134/running-command-line-silently-with-vbscript-and-getting-output – Rich Mar 01 '14 at 00:52
  • or http://stackoverflow.com/a/4963209/603855 – Ekkehard.Horner Mar 01 '14 at 07:25

2 Answers2

3

This may be done with the Windows Script Host Exec command. StdOut, StdIn, and StdErr may all be accessed, and ERRORLEVEL is available when the command completes.

Dim strMessage, strScript, strStdErr, strStdOut
Dim oExec, oWshShell, intErrorLevel
Dim ComSpec

Set oWshShell = CreateObject("WScript.Shell")
ComSpec = oWshShell.ExpandEnvironmentStrings("%comspec%")

intErrorLevel = 0
strScript = ComSpec & " /C echo %temp%"

On Error Resume Next
Set oExec = oWshShell.Exec (strScript)
If (Err.Number <> 0) Then
  strMessage = "Error: " & Err.Message
  intErrorLevel = 1
Else
  Do While oExec.Status = 0
    Do While Not oExec.StdOut.AtEndOfStream
      strStdOut = strStdOut & oExec.StdOut.ReadLine & vbCrLf
    Loop
    Do While Not oExec.StdErr.AtEndOfStream
      strStdErr = strStdErr & oExec.StdErr.ReadLine & vbCrLf
    Loop
    WScript.Sleep 0
  Loop
  intErrorLevel = oExec.ExitCode
  strMessage = strStdOut & strStdErr & CStr(intErrorLevel)
End If

WScript.Echo (strMessage)

NOTE: Replacing "ReadLine" above with "Read(1)" accomplishes the same thing, but adds an ability to process characters rather than whole lines.

kbulgrien
  • 4,384
  • 2
  • 26
  • 43
2

Of course Wscript.Shell would be a lot easier, but, since you want more fine grain control of your session, consider using Win32_Process. Usually, one uses this to control the placement of a new window, but, in your case, you want it hidden, so I set startupInfo.ShowWindow = 0 which means SW_HIDE. The following declares a VBScript function called RunCmd and which will run a command in an invisible window saving the output to a text file and then return the contents of the text file to the caller. As an example, I invoke RunCmd with the HOSTNAME command:

Function RunCmd(strCmd)
  Dim wmiService
  Set wmiService = GetObject("winmgmts:\\.\root\cimv2")
  Dim startupInfo
  Set startupInfo = wmiService.Get("Win32_ProcessStartup")
  Dim fso
  Set fso = CreateObject("Scripting.FileSystemObject")
  Dim cwd
  cwd = fso.GetAbsolutePathname(".")
  startupInfo.SpawnInstance_
  startupInfo.ShowWindow = 0
  ' startupInfo.X = 50
  ' startupInfo.y = 50
  ' startupInfo.XSize = 150
  ' startupInfo.YSize = 50
  ' startupInfo.Title = "Hello"
  ' startupInfo.XCountChars = 36
  ' startupInfo.YCountChars = 1
  Dim objNewProcess
  Set objNewProcess = wmiService.Get("Win32_Process")
  Dim intPID
  Dim errRtn
  errRtn = objNewProcess.Create("cmd.exe /c """ & strCmd & """ > out.txt", cwd, startupInfo, intPID)
  Dim f
  Set f = fso.OpenTextFile("out.txt", 1)
  RunCmd = f.ReadAll
  f.Close
End Function

MsgBox RunCmd("HOSTNAME")

References:

Stephen Quan
  • 21,481
  • 4
  • 88
  • 75
  • I wanted to stay clear from making txt files. Would have thought there would be a way to output to variable instead of txt file. – raylward102 Mar 05 '14 at 18:00