• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to execute dos cmds in the prompt opened using VBScript?

A

AuHuman

#1
Dim shell
Dim oExec
Set shell = CreateObject("wscript.shell")
Set oExec = shell.Exec("cmd.exe")
oExec.StdIn.Write("exit")
oExec.StdIn.Write VbCrLf
Do While oExec.Status = 0
WScript.Sleep 100
Loop
WScript.Echo oExec.Status
Above code is not executing the exit command in the command prompt opened by
shell.Exec("cmd.exe").
Nothing is happening until i close the prompt manually by clicking the close
button.
How to execute dos commands in the prompt opened by shell.Exec("cmd.exe") ?.
[i looked into the msdn library but its not clearing my above doubt]
 

My Computer

T

Tom Lavedas

#2
On Jan 26, 3:09 pm, AuHuman <AuHu...@newsgroup> wrote:

> Dim shell
> Dim oExec
> Set shell = CreateObject("wscript.shell")
> Set oExec = shell.Exec("cmd.exe")
> oExec.StdIn.Write("exit")
> oExec.StdIn.Write VbCrLf
> Do While oExec.Status = 0
> WScript.Sleep 100
> Loop
> WScript.Echo oExec.Status
> Above code is not executing the exit command in the command prompt openedby
> shell.Exec("cmd.exe").
> Nothing is happening until i close the prompt manually by clicking the close
> button.
> How to execute dos commands in the prompt opened by shell.Exec("cmd.exe")?.
> [i looked into the msdn library but its not clearing my above doubt]
This worked for me ...

Dim shell, oExec
Const delay = 5
Set shell = CreateObject("wscript.shell")
Set oExec = shell.Exec("cmd.exe /k echo Will close in " & delay & "
second(s)>con")
for n = delay to 1 step - 1
WScript.Sleep 1000
oExec.StdIn.Writeline "echo " & n & " > con"
Next
oExec.StdIn.Writeline "echo Closing > con"
WScript.Sleep 1000
oExec.StdIn.Writeline "exit"
WScript.Echo oExec.Status
_____________________
Tom Lavedas
 

My Computer

A

AuHuman

#3
Thanks a lot Tom Lavedas. Your post showed me the right way of resolving the
error i was facing.

"Tom Lavedas" wrote:

> On Jan 26, 3:09 pm, AuHuman <AuHu...@newsgroup> wrote:

> > Dim shell
> > Dim oExec
> > Set shell = CreateObject("wscript.shell")
> > Set oExec = shell.Exec("cmd.exe")
> > oExec.StdIn.Write("exit")
> > oExec.StdIn.Write VbCrLf
> > Do While oExec.Status = 0
> > WScript.Sleep 100
> > Loop
> > WScript.Echo oExec.Status
> > Above code is not executing the exit command in the command prompt opened by
> > shell.Exec("cmd.exe").
> > Nothing is happening until i close the prompt manually by clicking the close
> > button.
> > How to execute dos commands in the prompt opened by shell.Exec("cmd.exe") ?.
> > [i looked into the msdn library but its not clearing my above doubt]
>
> This worked for me ...
>
> Dim shell, oExec
> Const delay = 5
> Set shell = CreateObject("wscript.shell")
> Set oExec = shell.Exec("cmd.exe /k echo Will close in " & delay & "
> second(s)>con")
> for n = delay to 1 step - 1
> WScript.Sleep 1000
> oExec.StdIn.Writeline "echo " & n & " > con"
> Next
> oExec.StdIn.Writeline "echo Closing > con"
> WScript.Sleep 1000
> oExec.StdIn.Writeline "exit"
> WScript.Echo oExec.Status
> _____________________
> Tom Lavedas
> .
>
 

My Computer

M

mr_unreliable

#4
Tom Lavedas wrote:

> This worked for me ...
> ...
> Set oExec = shell.Exec("cmd.exe /k echo Will close in " & delay & "
> ...
Suggest using:

Set oExec = shell.Exec("%comspec% /k echo Will (blah, blah,.. )

instead of:

Set oExec = shell.Exec("cmd.exe /k echo Will (blah, blah,.. )


cheers, nitpickers anonymous
 

My Computer

T

Tom Lavedas

#5
On Jan 31, 11:58 am, mr_unreliable
<kindlyReplyToNewsgr...@newsgroup> wrote:

> Tom Lavedas wrote:

> > This worked for me ...
> > ...
> > Set oExec = shell.Exec("cmd.exe /k echo Will close in " & delay & "
> > ...
>
> Suggest using:
>
>    Set oExec = shell.Exec("%comspec% /k echo Will (blah, blah,.. )
>
> instead of:
>
>    Set oExec = shell.Exec("cmd.exe /k echo Will (blah, blah,.. )
>
> cheers, nitpickers anonymous
A nit picking Win98 user. I wonder how many of you there are
left ;^))

I always use COMSPEC, myself, but chose to stick with the formulation
used by the OP in this case.
_____________________
Tom Lavedas
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)