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 - Exec Method Puzzle

Reply
 
Old 07-16-2009   #1 (permalink)
Pegasus [MVP]


 
 

Exec Method Puzzle

Below is a stripped down version of a script I've been working on. It works
as expected when invoked with cscript.exe: It executes the nominated batch
file:

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWshShell = CreateObject("WScript.Shell")
sTemp = oWshShell.ExpandEnvironmentStrings("%Temp%\TempBat.bat")
Set oBatchFile = oFSO.CreateTextFile(sTemp, True)
oBatchFile.WriteLine "echo %date% %time% >> c:\test.txt"
oBatchFile.Close
Set oExec = oWshShell.exec(sTemp)
WScript.Sleep 1000

If I remove the Sleep method at the end then the script does not appear to
do anything. It runs but it does not generate an entry in the log file. The
real batch file contains several meaningful commands - none of them are
executed.

Could someone explain what difference the Sleep method makes?



My System SpecsSystem Spec
Old 07-16-2009   #2 (permalink)
Tom Lavedas


 
 

Re: Exec Method Puzzle

On Jul 16, 9:40*am, "Pegasus [MVP]" <n...@xxxxxx> wrote:
Quote:

> Below is a stripped down version of a script I've been working on. It works
> as expected when invoked with cscript.exe: It executes the nominated batch
> file:
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oWshShell = CreateObject("WScript.Shell")
> sTemp = oWshShell.ExpandEnvironmentStrings("%Temp%\TempBat.bat")
> Set oBatchFile = oFSO.CreateTextFile(sTemp, True)
> oBatchFile.WriteLine "echo %date% %time% >> c:\test.txt"
> oBatchFile.Close
> Set oExec = oWshShell.exec(sTemp)
> WScript.Sleep 1000
>
> If I remove the Sleep method at the end then the script does not appear to
> do anything. It runs but it does not generate an entry in the log file. The
> real batch file contains several meaningful commands - none of them are
> executed.
>
> Could someone explain what difference the Sleep method makes?
Without the Sleep, the calling script exits (clearing the oExec
object) before the underlying batch procedure can complete its
execution.

Note that the documentation illustrates the need to wait until the
Status property becomes 1. Therefore, a proper implementation would
be something like this ...

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWshShell = CreateObject("WScript.Shell")
sTemp = oWshShell.ExpandEnvironmentStrings("%Temp%\TempBat.bat")
Set oBatchFile = oFSO.CreateTextFile(sTemp, True)
oBatchFile.WriteLine "echo %date% %time% >> c:\test.txt"
oBatchFile.Close
Set oExec = oWshShell.exec(sTemp)
Do Until oExec.Status <> 0
WScript.Sleep 50
Loop

Tom Lavedas
***********
My System SpecsSystem Spec
Old 07-16-2009   #3 (permalink)
Pegasus [MVP]


 
 

Re: Exec Method Puzzle


"Tom Lavedas" <tglbatch@xxxxxx> wrote in message
news:70b00bbe-7e36-416d-a8fa-4ab87724d11f@xxxxxx
On Jul 16, 9:40 am, "Pegasus [MVP]" <n...@xxxxxx> wrote:
Quote:

> Below is a stripped down version of a script I've been working on. It
> works
> as expected when invoked with cscript.exe: It executes the nominated batch
> file:
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oWshShell = CreateObject("WScript.Shell")
> sTemp = oWshShell.ExpandEnvironmentStrings("%Temp%\TempBat.bat")
> Set oBatchFile = oFSO.CreateTextFile(sTemp, True)
> oBatchFile.WriteLine "echo %date% %time% >> c:\test.txt"
> oBatchFile.Close
> Set oExec = oWshShell.exec(sTemp)
> WScript.Sleep 1000
>
> If I remove the Sleep method at the end then the script does not appear to
> do anything. It runs but it does not generate an entry in the log file.
> The
> real batch file contains several meaningful commands - none of them are
> executed.
>
> Could someone explain what difference the Sleep method makes?
Without the Sleep, the calling script exits (clearing the oExec
object) before the underlying batch procedure can complete its
execution.

Note that the documentation illustrates the need to wait until the
Status property becomes 1. Therefore, a proper implementation would
be something like this ...

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWshShell = CreateObject("WScript.Shell")
sTemp = oWshShell.ExpandEnvironmentStrings("%Temp%\TempBat.bat")
Set oBatchFile = oFSO.CreateTextFile(sTemp, True)
oBatchFile.WriteLine "echo %date% %time% >> c:\test.txt"
oBatchFile.Close
Set oExec = oWshShell.exec(sTemp)
Do Until oExec.Status <> 0
WScript.Sleep 50
Loop

Tom Lavedas
***********

Thanks for your prompt reply. The documentation in script56.chm does
illustrate the wait loop but unfortunately it never explains the reason for
its existence. Your response makes it clear why the loop must be there.


My System SpecsSystem Spec
Old 07-16-2009   #4 (permalink)
Tom Lavedas


 
 

Re: Exec Method Puzzle

On Jul 16, 11:56*am, "Pegasus [MVP]" <n...@xxxxxx> wrote:
Quote:

> "Tom Lavedas" <tglba...@xxxxxx> wrote in message
>
> news:70b00bbe-7e36-416d-a8fa-4ab87724d11f@xxxxxx
> On Jul 16, 9:40 am, "Pegasus [MVP]" <n...@xxxxxx> wrote:
>
{snip}
Quote:

>
> Thanks for your prompt reply. The documentation in script56.chm does
> illustrate the wait loop but unfortunately it never explains the reason for
> its existence. Your response makes it clear why the loop must be there.
Yes, the documentation leaves a lot to be desired in some respects.

Glad I could help.

Tom Lavedas
My System SpecsSystem Spec
Old 07-17-2009   #5 (permalink)
Joe Fawcett


 
 

Re: Exec Method Puzzle

It's often the case that things are better after a good sleep

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

"Pegasus [MVP]" <news@xxxxxx> wrote in message
news:OumV5rhBKHA.4608@xxxxxx
Quote:

> Below is a stripped down version of a script I've been working on. It
> works as expected when invoked with cscript.exe: It executes the nominated
> batch file:
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oWshShell = CreateObject("WScript.Shell")
> sTemp = oWshShell.ExpandEnvironmentStrings("%Temp%\TempBat.bat")
> Set oBatchFile = oFSO.CreateTextFile(sTemp, True)
> oBatchFile.WriteLine "echo %date% %time% >> c:\test.txt"
> oBatchFile.Close
> Set oExec = oWshShell.exec(sTemp)
> WScript.Sleep 1000
>
> If I remove the Sleep method at the end then the script does not appear to
> do anything. It runs but it does not generate an entry in the log file.
> The real batch file contains several meaningful commands - none of them
> are executed.
>
> Could someone explain what difference the Sleep method makes?
>


My System SpecsSystem Spec
Old 07-17-2009   #6 (permalink)
Pegasus [MVP]


 
 

Re: Exec Method Puzzle


"Joe Fawcett" <joefawcett@xxxxxx> wrote in message
news:uRr1dFsBKHA.4792@xxxxxx
Quote:

> It's often the case that things are better after a good sleep
LOL!


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Passing "| find" in command executed with EXEC method does not work VB Script
Puzzle Vista music pictures video
Method invocation failed because [System.String] doesn't contain a method PowerShell
Can't exec a program from another Vista security
Fun Puzzle? Vista mail


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