vbs how to get result from a command line command

10,435

When in doubt, read the documentation. You probably want something like this:

Set p = CreateObject("WScript.Shell").Exec("%COMSPEC% /c date /t")
Do While p.Status = 0
  WScript.Sleep 100
Loop
WScript.Echo p.StdOut.ReadAll

Edit: When using Exec() you pass input via the .StdIn descriptor, not via SendKeys() (which is a rather unreliable way of passing input anyway).

%COMSPEC% is a system environment variable with the full path to cmd.exe and the /c option makes cmd.exe exit after the command (date /t in the example) is finished.

If the command indicates success/failure with an exit code, you can check the ExitCode property after the command finished.

If p.Status <> 0 Then WScript.Echo p.ExitCode

Edit2: Instead of using atprogram interactively, can you construct commandlines that will perform particular tasks without user interaction? With non-interactive commandlines something like this might work:

prompt = "C:\>"
atprogram_cmdline_1 = "atprogram.exe ..."
atprogram_cmdline_2 = "atprogram.exe ..."
'...

Function ReadOutput(p)
  text = ""
  Do Until Right(text, Len(prompt)) = prompt
    text = text & p.StdOut.Read(1)
  Loop
  ReadOutput = text
End Function

Set cmd = CreateObject("WScript.Shell").Exec("%COMSPEC% /k")
ReadOutput cmd  ' skip over first prompt

cmd.StdIn.WriteLine(atprogram_cmdline_1)
WScript.Echo ReadOutput(cmd)
cmd.StdIn.WriteLine(atprogram_cmdline_2)
WScript.Echo ReadOutput(cmd)
'...

cmd.Terminate  ' exit CMD.EXE

%COMSPEC% /k spawns a command prompt without running a command. The /k prevents it from closing. Because it isn't closing automatically, you can't use the While p.Status = 0 loop here. If a command needs some time to finish, you need to WScript.Sleep a number of seconds.

Via cmd.StdIn.WriteLine you can run commandlines in the CMD instance. The function ReadOutput() reads the output from StdOut until the next prompt appears. You need to look for the prompt, because read operations are blocking, so you can't simply say "read all that's been printed yet".

After you're finished you quit CMD.EXE via cmd.Terminate.

Share:
10,435
user1387865
Author by

user1387865

Updated on June 12, 2022

Comments

  • user1387865
    user1387865 3 months

    I want to get the result of a simple command from the command line (cmd.exe) using a Windows script (.vbs). How is this done? I haven't been able to find a good/simple example or explanation. You could use the "date" or "time" command to provide an example with. Such as:

    enter image description here

    P.S. I am able to write the script code that opens cmd.exe and sends the command.

    Thanks!

  • user1387865
    user1387865 almost 10 years
    Okay that works, thanks, but I guess I wasn't expecting such a streamlined answer. I'll just explain what I'm trying to do. I want to use Atmel's command line based programming to program some chips. It's really easy to use and works well. I am able to bring up the command shell and send it the commands using SendKeys, but when I try to include the command using Exec it just brings up the window but doesn't insert the command. What is the "/c" actually doing? Is there another way to get the output of a command? I need a way to know if the programming was successful or not.
  • user1387865
    user1387865 almost 10 years
    Thanks for the help! I appreciate it. I just haven't been successful yet. For some reason, when I send <code>Set p = CreateObject("WScript.Shell").Exec("%COMSPEC% /c atprogram")</code> it just opens the terminal without entering the command "atprogram". I've tried several things but it just won't run the command. I feel like I am forced to use SendKeys but then I can't see the output unless I leave the terminal window open.