Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

assign output of a command to a string variable 1

Status
Not open for further replies.

pho01

Programmer
Joined
Mar 17, 2003
Messages
218
Location
US
How do I assign a multiple line output of a dos command to a string variable?

For example: I want to assign the string variable "output" = output of the dos dir command

thanks!
 
It will be a lot easier to use the Dir statement in VB rather tahn get the output from a DOS command.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
John is right.
However, if you need (or want) a DOS command anyway, here's a way to achieve this:

Code:
Public Function GetCommandLine(strCommand As String) As String
    Dim myExec As Object
    Dim result As String
    
    With CreateObject("WScript.shell")
        Set myExec = .Exec("cmd /c " & strCommand)
    End With
    Do While myExec.Status = WshRunning
        GetCommandLine = GetCommandLine & myExec.StdOut.ReadAll
        DoEvents
    Loop
    GetCommandLine = GetCommandLine & myExec.StdOut.ReadAll 'Make sure we've got the lot
End Function

You can then get the output from anywhere in your code, simply by using the command
Code:
myOutput = GetCommandLine("your dos command goes here")
P.S: Credits go to someone here on TT, who provided this code originally. Too bad I don't remember who it was. Perhaps Skip? He's always good for snippets like this.
:-)

[blue]Help us, join us, participate
IAHRA - International Alliance of Human Rights Advocates[/blue]
 
No, this particular version was me; the comment on the last line is the give away
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top