Re: Assigning variable output of %comspec% by Michael
Michael
Fri Jun 15 09:23:43 CDT 2007
mik357@hotmail.com wrote:
> Hello,
>
> Is there a way to allow a variable to hold the output of the comspec
> statement (the statement running commands on command prompt)?
>
> The command I am running produces result of only a single word on
> screen, so can I:
>
> VAR = objShell.run 'The comspec statement
>
> Is this possible? I get an error when I do this but I really want the
> variable to hold the value, is there any other way?
This is common function that I use. It returns the captured output as an
array of lines. You'll find lots of variations on the technique in the
*.scripting newsgroups.
a = runCMD("dir *.*")
wscript.echo Join(a,vbcrlf)
'=======================================================================
'
'=======================================================================
Function runCMD(argCmd)
Dim fso, shell
Dim sCmd, sTempfile
Dim arLines
set fso = createobject("scripting.filesystemobject")
set shell = createobject("wscript.shell")
sCmd = """%comspec%"" /c " & argCmd & " >"
sTempfile = fso.GetTempName
sTempfile = shell.Environment("PROCESS")("TEMP") & "\" & sTempfile
shell.Run sCmd & Chr(34) & sTempfile & Chr(34), 0, true
arLines = Split(fso.OpenTextFile(sTempfile).ReadAll, _
vbNewline)
ReDim Preserve arLines(Ubound(arLines) - 1)
fso.DeleteFile sTempfile
runCMD = arLines
End Function
--
Michael Harris
Microsoft.MVP.Scripting