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?

Re: Assigning variable output of %comspec% by ekkehard

ekkehard
Fri Jun 15 08:29:58 CDT 2007

mik357@hotmail.com schrieb:
> 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?
>
Straight from the VBScript Docs:

set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "WinDir is " & WshShell.ExpandEnvironmentStrings("%WinDir%")

(the sample code for ExpandEnvironmentStrings())

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



Re: Assigning variable output of %comspec% by TDM

TDM
Fri Jun 15 09:24:44 CDT 2007


<mik357@hotmail.com> wrote in message
news:1181912241.521228.139830@m36g2000hse.googlegroups.com...
> 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?
>

If you want to use the .Exec method instead of the .Run method,
then you will have access to StdOut which should contain your
single word This example has a one word output similar to what
your app might do.

Dim objSH
Dim objRtn
Dim strMyVar

Set objSH = CreateObject("WScript.Shell")

Set objRtn = objSH.Exec("%comspec% /c echo spud")

Do While Not objRtn.StdOut.AtEndOfStream
strMyVar = objRtn.StdOut.ReadLine
WScript.Echo strMyVar
Loop

TDM



Re: Assigning variable output of %comspec% by Harvey

Harvey
Fri Jun 15 09:35:36 CDT 2007

You can redirect the output of a CMD line program to a log file. Or you
could use ".Exec" method to control Standard In/Out/Error data streams for
the CMD Line program.


NOTE: Setting a variable to the results of a CMD line program retrieve it's
"return code" (a.k.a. errorlevel number)

Example 1.

oWS.Run("%comspec% /c dir C:\ > dir.log",2, True)

Example 2.

Set oWS = WScript.CreateObject("WScript.Shell")

Set oExec = oWS.Exec("nslookup.exe")

oExec.stdIn.WriteLine "www.microsoft.com"

oExec.stdIn.WriteLine "exit"

sResults = oExec.stdOut.ReadAll

MsgBox sResults





<mik357@hotmail.com> wrote in message
news:1181912241.521228.139830@m36g2000hse.googlegroups.com...
> 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?
>


Re: Assigning variable output of %comspec% by mik357

mik357
Fri Jun 15 09:58:08 CDT 2007

I tried using TDM's/Havey's method, it works but this is the twist:

The command I am running first executes a separate command prompt
window and in that (new) command prompt, it executes the command which
gives the output. But the method given to me just captures the output
from the original window which gives me nothing as result.

Tell me, if it is easier, the command prompt in question is a special
one (similar but customized by vendor). Is there any way I can run
that specific command prompt instead of the default one?


Re: Assigning variable output of %comspec% by TDM

TDM
Fri Jun 15 10:16:53 CDT 2007


<mik357@hotmail.com> wrote in message
news:1181919488.083350.260350@w5g2000hsg.googlegroups.com...
>I tried using TDM's/Havey's method, it works but this is the twist:
>
> The command I am running first executes a separate command prompt
> window and in that (new) command prompt, it executes the command which
> gives the output. But the method given to me just captures the output
> from the original window which gives me nothing as result.
>
> Tell me, if it is easier, the command prompt in question is a special
> one (similar but customized by vendor). Is there any way I can run
> that specific command prompt instead of the default one?
>

That is a twist isn't it. My guess is the you will have to try and find
the execution for the second shell. You say "the command I am
running", what exactly is that? Is is a batch file ?, another script ?
If so, you can take a look at that file to see what is being run and
run that directly. But this is likely not the option or you would have
figured that out already is my guess. If it is an executable, you may
be out of luck.


TDM