Tom
Wed Mar 03 14:00:47 CST 2004
No, not with the Run method. However, the Exec method of
WSH v5.6 could be used to do that. The tradeoff is that
Exec MUST open a command prompt console window to do it.
Therefore, I personally use the Run method as in Togeir's
example.
For completeness, the Exec approach could be something
like this ...
sExe = "ipconfig.exe /all"
with CreateObject("WScript.Shell")
with .Exec(sExe)
with .StdOut
do until .AtEndofStream
s = s & Replace(.ReadLine, vbcr, "") _
& vbNewLine
Loop
end with ' StdOut
end with ' Exec
end with ' Shell
wsh.echo s
If you don't mind the console flash, you could use this
approach.
Tom Lavedas
===========
>-----Original Message-----
>Hello Torgeir,
>
>Thank you very much. This is exactly what I'm looking for.
>
>Out of my curiousity. Instead of writing the output to
the file, then read
>the file into a variable, can we capture the output
directly to a variable?
>
>Thanks again,
>Peter Nguyen
>
>
>"Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com>
wrote in message
>news:4045271C.FEA01A9A@hydro.com...
>> Peter Nguyen wrote:
>>
>> > Is there anyway for me to capture the output of the
oShell.Run command
>and
>> > assign it to a variable?
>> >
>> > I want to run IPCONFIG /ALL and get the output dumps
into a variable, so
>I
>> > tried to do this but none of them work:
>> >
>> > 1. Output the command results to textfile.
>> >
>> > Set oShell = createObject("WScript.Shell")
>> > oShell.Run ("ipconfig /all > c:\output.txt")
>>
>> Hi
>>
>> As it is the command processor that supports
redirection, you need to add
>that
>> to your code:
>>
>> oShell.Run ("%comspec% /c ipconfig /all >
c:\output.txt")
>>
>>
>> Here is an example playing a bit with ipconfig's output:
>>
>>
>> Const OpenAsASCII = 0
>> Const FailIfNotExist = 0
>> Const ForReading = 1
>>
>> sExe = "ipconfig.exe /all"
>>
>> Set oShell = CreateObject("WScript.Shell")
>> Set oFSO = CreateObject("Scripting.FileSystemObject")
>> sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
>> sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" &
oFSO.GetTempName
>>
>> oShell.Run "%comspec% /c " & sExe & " >" & sTempFile,
0 , True
>>
>> Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
>> FailIfNotExist,
OpenAsASCII)
>>
>> ' get ipconfig's output into a variable.
>> sResults = fFile.ReadAll
>> fFile.Close
>> oFSO.DeleteFile(sTempFile)
>>
>> ' ipconfig's output contains some vbCr characters that
can give
>> ' some unwanted side effects, removing them using
Replace.
>> sResults = Replace(sResults, vbCr, "")
>>
>> ' create an array of the output.
>> aResult = Split(sResults, vbCrLf)
>> For i = 0 To UBound(aResult)
>> WScript.Echo aResult(i)
>> Next
>>
>>
>>
>>
>> --
>> torgeir
>> Microsoft MVP Scripting and WMI, Porsgrunn Norway
>> Administration scripting examples and an ONLINE version
of the 1328 page
>> Scripting Guide:
>>
http://www.microsoft.com/technet/community/scriptcenter/def
ault.mspx
>>
>>
>
>
>.
>