Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8623241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:14:12+00:00 2026-06-12T07:14:12+00:00

I want to get the result of a simple command from the command line

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:14:13+00:00Added an answer on June 12, 2026 at 7:14 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get the result for the 3 most recent months. and this
I have trouble doing this... You may want to get the same result as
I want to run a simple one-liner in the Windows CMD prompt to print
I want to get response from websites that take a simple input, which is
page contents: aa<b>1;2'3</b>hh<b>aaa</b>.. .<b>bbb</b> blabla.. i want to get result: 1;2'3aaabbb match tag is
I want to get the e.result before it pumps data into my listbox..I want
i search online but can't get satisfactory result i want to protect images on
Given a string string result = "01234" I want to get the separate integers
I want to get results from sparql query and the results contain no namespace.
I want to get randomized results for a query. I read that using RAND()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.