Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Shell.Run problem

Reply
 
Old 01-29-2009   #1 (permalink)
Peter Gordon


 
 

Shell.Run problem

The two lines of code below, will open a console window,
run the dir command and then close the window before
the contents can be read. Is there any way to keep the
window open and displaying it's contents?

The code:
Set aShell = Wscript.CreateObject("Wscript.Shell")
iret = aShell.Run( "dir", 1, TRUE)


--
Peter Gordon

My System SpecsSystem Spec
Old 01-29-2009   #2 (permalink)
Pegasus \(MVP\)


 
 

Re: Shell.Run problem


"Peter Gordon" <petergo@xxxxxx> wrote in message
news:Xns9BA3574629C80petergonetspacenetau@xxxxxx
Quote:

> The two lines of code below, will open a console window,
> run the dir command and then close the window before
> the contents can be read. Is there any way to keep the
> window open and displaying it's [its] contents?
>
> The code:
> Set aShell = Wscript.CreateObject("Wscript.Shell")
> iret = aShell.Run( "dir", 1, TRUE)
>
>
> --
> Peter Gordon
Unfortunately your code won't run anything because there is no executable
called "dir.exe" or "dir.com". "Dir" is an internal Command Processor
command, which means that you must launch cmd.exe to run it, as for some
other commands such as cd, md, del, copy. Try this code:

Set oShell = WScript.CreateObject("Wscript.Shell")
iret = oShell.run("cmd.exe /c dir c:\ & pause", 1, True)

To prevent the Console process from closing, add the "pause" command.


My System SpecsSystem Spec
Old 01-29-2009   #3 (permalink)
Dirk Stegemann


 
 

Re: Shell.Run problem

Hi Peter,

better try this one :-)

--
Set aShell = Wscript.CreateObject("Wscript.Shell")
iret = aShell.Run("cmd /K CD C:\ & Dir")
MsgBox iret
--
Dirk

"Peter Gordon" <petergo@xxxxxx> schrieb im Newsbeitrag
news:Xns9BA3574629C80petergonetspacenetau@xxxxxx
Quote:

> The two lines of code below, will open a console window,
> run the dir command and then close the window before
> the contents can be read. Is there any way to keep the
> window open and displaying it's contents?
>
> The code:
> Set aShell = Wscript.CreateObject("Wscript.Shell")
> iret = aShell.Run( "dir", 1, TRUE)
>
>
> --
> Peter Gordon

My System SpecsSystem Spec
Old 01-29-2009   #4 (permalink)
Peter Gordon


 
 

Re: Shell.Run problem

"Pegasus \(MVP\)" <I.can@xxxxxx> wrote in
news:OLHFYSmgJHA.3708@xxxxxx:
Quote:

>
> "Peter Gordon" <petergo@xxxxxx> wrote in message
> news:Xns9BA3574629C80petergonetspacenetau@xxxxxx
Quote:

>> The two lines of code below, will open a console window,
>> run the dir command and then close the window before
>> the contents can be read. Is there any way to keep the
>> window open and displaying it's [its] contents?
>>
>> The code:
>> Set aShell = Wscript.CreateObject("Wscript.Shell")
>> iret = aShell.Run( "dir", 1, TRUE)
>>
>>
>> --
>> Peter Gordon
>
> Unfortunately your code won't run anything because there is no
> executable called "dir.exe" or "dir.com". "Dir" is an internal Command
> Processor command, which means that you must launch cmd.exe to run it,
> as for some other commands such as cd, md, del, copy. Try this code:
>
> Set oShell = WScript.CreateObject("Wscript.Shell")
> iret = oShell.run("cmd.exe /c dir c:\ & pause", 1, True)
>
> To prevent the Console process from closing, add the "pause" command.
>
>
>
Sorry, I caught myself when trying to simplify my request.
I've trying to write a script for backing folders on my computer
using xxcopy
A typical command is:

C:\Windows\System32\xxcopy.exe D:\Notes F:\D_Drive\Notes /BI /Y /KS /E
/R /Q

This backs up D:\Notes to F:\D_Drive\Notes using the options I require.

Using an input box, I select the folder to be backed up and the
script then runs the appropriate command. It works fine, except for
the fact that the cmd window closes before I can read the output from
xxcopy. If I open a cmd.exe shell as you have suggested, I cannot input
my
command into that shell. At the moment, I have a .cmd file of the
commands and a pause command in the .cmd file. I then use vbscript
to execute the .cmd file. This works fine & the window remains open
until I press a key so I can read the xxcopy output. Okay, it works
but it is ugly. Is there a more elegant method?

Sorry, if I have now over complicated the question.


--
Peter Gordon
My System SpecsSystem Spec
Old 01-29-2009   #5 (permalink)
Richard Mueller [MVP]


 
 

Re: Shell.Run problem


"Peter Gordon" <petergo@xxxxxx> wrote in message
news:Xns9BA3574629C80petergonetspacenetau@xxxxxx
Quote:

> The two lines of code below, will open a console window,
> run the dir command and then close the window before
> the contents can be read. Is there any way to keep the
> window open and displaying it's contents?
>
> The code:
> Set aShell = Wscript.CreateObject("Wscript.Shell")
> iret = aShell.Run( "dir", 1, TRUE)
>
>
> --
> Peter Gordon
Best is actually:

iret = aShell.Run("%comspec% /k dir c:\", 1, True)

or

iret = aShell.Run("%comspec% /c dir c:\ & pause", 1, True)

The %comspec% environment variable refers to the correct command depending
on the operating system.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 01-29-2009   #6 (permalink)
Peter Gordon


 
 

Re: Shell.Run problem

"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
news:ennnjGngJHA.4556@xxxxxx:
Quote:

>
> "Peter Gordon" <petergo@xxxxxx> wrote in message
> news:Xns9BA3574629C80petergonetspacenetau@xxxxxx
Quote:

>> The two lines of code below, will open a console window,
>> run the dir command and then close the window before
>> the contents can be read. Is there any way to keep the
>> window open and displaying it's contents?
>>
>> The code:
>> Set aShell = Wscript.CreateObject("Wscript.Shell")
>> iret = aShell.Run( "dir", 1, TRUE)
>>
>>
>> --
>> Peter Gordon
>
> Best is actually:
>
> iret = aShell.Run("%comspec% /k dir c:\", 1, True)
>
> or
>
> iret = aShell.Run("%comspec% /c dir c:\ & pause", 1, True)
>
> The %comspec% environment variable refers to the correct command
> depending on the operating system.
>
How do I run:
ret = xxcopyShell.Run( "xxcopy D:\Notes F:\Notes /BI", 1, TRUE)
and make the shell pause, (i.e remain open) after the command is
executed?


--
Peter Gordon
My System SpecsSystem Spec
Old 01-29-2009   #7 (permalink)
Richard Mueller [MVP]


 
 

Re: Shell.Run problem


"Peter Gordon" <petergo@xxxxxx> wrote in message
news:Xns9BA374154A9B6petergonetspacenetau@xxxxxx
Quote:

> "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
> news:ennnjGngJHA.4556@xxxxxx:
>
Quote:

>>
>> "Peter Gordon" <petergo@xxxxxx> wrote in message
>> news:Xns9BA3574629C80petergonetspacenetau@xxxxxx
Quote:

>>> The two lines of code below, will open a console window,
>>> run the dir command and then close the window before
>>> the contents can be read. Is there any way to keep the
>>> window open and displaying it's contents?
>>>
>>> The code:
>>> Set aShell = Wscript.CreateObject("Wscript.Shell")
>>> iret = aShell.Run( "dir", 1, TRUE)
>>>
>>>
>>> --
>>> Peter Gordon
>>
>> Best is actually:
>>
>> iret = aShell.Run("%comspec% /k dir c:\", 1, True)
>>
>> or
>>
>> iret = aShell.Run("%comspec% /c dir c:\ & pause", 1, True)
>>
>> The %comspec% environment variable refers to the correct command
>> depending on the operating system.
>>
>
> How do I run:
> ret = xxcopyShell.Run( "xxcopy D:\Notes F:\Notes /BI", 1, TRUE)
> and make the shell pause, (i.e remain open) after the command is
> executed?
>
>
> --
> Peter Gordon
I would use:
========
Set aShell = Wscript.CreateObject("Wscript.Shell")
strCmd = "%comspec% /k xcopy ""D:\Notes"" ""F:\Notes"" /BI"
Wscript.Echo strCmd
ret = aShell.Run(strCmd, 1, True)
========
I assume you meant the xcopy command. Note that I enclosed the paths in
quotes in case there are any spaces. In a quoted string (like the value of
the strCmd variable) any embedded double quotes must be doubled. I echo the
command line to the console so I can see what it looks like and verify that
it is correct. The /k parameter keeps the command shell open until you exit.
Finally, it might make sense to use RoboCopy instead.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 02-05-2009   #8 (permalink)
Al Dunbar


 
 

Re: Shell.Run problem


"Pegasus (MVP)" <I.can@xxxxxx> wrote in message
news:OLHFYSmgJHA.3708@xxxxxx
Quote:

>
> "Peter Gordon" <petergo@xxxxxx> wrote in message
> news:Xns9BA3574629C80petergonetspacenetau@xxxxxx
Quote:

>> The two lines of code below, will open a console window,
>> run the dir command and then close the window before
>> the contents can be read. Is there any way to keep the
>> window open and displaying it's [its] contents?
>>
>> The code:
>> Set aShell = Wscript.CreateObject("Wscript.Shell")
>> iret = aShell.Run( "dir", 1, TRUE)
>>
>>
>> --
>> Peter Gordon
>
> Unfortunately your code won't run anything because there is no executable
> called "dir.exe" or "dir.com". "Dir" is an internal Command Processor
> command, which means that you must launch cmd.exe to run it, as for some
> other commands such as cd, md, del, copy.
Unless, of course, there is a dir.bat or dir.cmd file...
Quote:

> Try this code:
>
> Set oShell = WScript.CreateObject("Wscript.Shell")
> iret = oShell.run("cmd.exe /c dir c:\ & pause", 1, True)
>
> To prevent the Console process from closing, add the "pause" command.
Another method would be this:

Set oShell = WScript.CreateObject("Wscript.Shell")
iret = oShell.run("cmd.exe /k dir c:\", 1, True)

this will keep the command window open and usable, allowing the user to
examine environment variables or what have you before closing the window
with exit.

/Al


My System SpecsSystem Spec
Old 02-05-2009   #9 (permalink)
Peter Gordon


 
 

Re: Shell.Run problem

"Al Dunbar" <alandrub@xxxxxx> wrote in
news:e2S2u60hJHA.4596@xxxxxx:
Quote:

>
> "Pegasus (MVP)" <I.can@xxxxxx> wrote in message
> news:OLHFYSmgJHA.3708@xxxxxx
Quote:

>>
>> "Peter Gordon" <petergo@xxxxxx> wrote in message
>> news:Xns9BA3574629C80petergonetspacenetau@xxxxxx
Quote:

>>> The two lines of code below, will open a console window,
>>> run the dir command and then close the window before
>>> the contents can be read. Is there any way to keep the
>>> window open and displaying it's [its] contents?
>>>
>>> The code:
>>> Set aShell = Wscript.CreateObject("Wscript.Shell")
>>> iret = aShell.Run( "dir", 1, TRUE)
>>>
>>>
>>> --
>>> Peter Gordon
>>
>> Unfortunately your code won't run anything because there is no
>> executable called "dir.exe" or "dir.com". "Dir" is an internal
>> Command Processor command, which means that you must launch cmd.exe
>> to run it, as for some other commands such as cd, md, del, copy.
>
> Unless, of course, there is a dir.bat or dir.cmd file...
>
Quote:

>> Try this code:
>>
>> Set oShell = WScript.CreateObject("Wscript.Shell")
>> iret = oShell.run("cmd.exe /c dir c:\ & pause", 1, True)
>>
>> To prevent the Console process from closing, add the "pause" command.
>
> Another method would be this:
>
> Set oShell = WScript.CreateObject("Wscript.Shell")
> iret = oShell.run("cmd.exe /k dir c:\", 1, True)
>
> this will keep the command window open and usable, allowing the user
> to examine environment variables or what have you before closing the
> window with exit.
>
> /Al
Thanks for the replies, I eventually used the code below
which works well.

Set xxcopyShell = Wscript.CreateObject("Wscript.Shell")
strXXCOPY = "%comspec% /c xxcopy.exe "
strOptions = " /BI /Y /KS /E /R /X*\Tmp\ /Q "
strCmd = strXXCOPY & strOptions & strFiles & " & pause"
' MsgBox strCmd, vbOkOnly, "The Command"
ret = xxcopyShell.Run( strCmd, 1, TRUE)


--
Peter Gordon
My System SpecsSystem Spec
Old 02-07-2009   #10 (permalink)
Amin Pourhadi


 
 

Re: Shell.Run problem

hi all

my problem is like this subject.
when you run your script from 25*80 (full screen mode) command prompt, &
your script call a win app (such as notepad) you can't go back to
command prompt automaticly & need to push "enter" key for this. why or
how to do it?




*** Sent via Developersdex http://www.developersdex.com ***
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Can parent shell monitor run/crash status of Child shell/exe ? PowerShell
Problem with WSH Shell Exec StdOut VB Script
problem with shell.application PowerShell
Executing Power Shell Scripts from Windows Shell PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46