Solved

start batch and hold script until it finishes

Posted on 2008-06-20
9
872 Views
Last Modified: 2011-10-19
hey,
i'm searching for a way to start a .bat file from a vbs script (which is started by a batch), let the vbs script monitor the batch file and continu only after the batch file has ended. the thing is that this batch file also starts a vbs... ;-)

schematic: bat1 starts vbs1, vbs1 starts bat2 which starts vbs2, vbs1 continues after bat2 (vbs2) has finished.

i tried many things already, amongst others:
A
For Each Process in GetObject( "winmgmts:").InstancesOf( "win32_process" )
but since the parent (vbs) as well as the child (bat) run in a cmd this is not enough...

B
active = WshShell.AppActivate(strTitle)
but this only works when starting ie. calc.exe directly. when i put the calc.exe in a batch-vbs file with a title (start /wait cmd /C "TITLE CalculatorVBS && cscript calc.vbs") it doesn't work no more.

(see the attached zip for examples, remove the txt extensions)

cheers,
S.


wait.zip
0
Comment
Question by:lynx7
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 21831474
How about
Dim objShell : Set objShell=CreateObject("Wscript.Shell")
objShell.Run "c:\folder\yourfile.bat",0,1

 Arguments
object
WshShell object.

strCommand
String value indicating the command line you want to run. You must include any parameters you want to pass to the executable file.

intWindowStyle
Optional. Integer value indicating the appearance of the program's window. Note that not all programs make use of this information.

bWaitOnReturn
Optional. Boolean value indicating whether the script should wait for the program to finish executing before continuing to the next statement in your script. If set to true, script execution halts until the program finishes, and Run returns any error code returned by the program. If set to false (the default), the Run method returns immediately after starting the program, automatically returning 0 (not to be interpreted as an error code).
ref: http://msdn.microsoft.com/en-us/library/d5fk67ky(VS.85).aspx
0
 
LVL 15

Expert Comment

by:sr75
ID: 21838593
Thanks SirBounty, I never knew about the WaitOnReturn option of objShell.Run.  I always used objShell.Exec() instead because it waited automatically.  It also captured the output which I may or may not need to do something with.
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 200 total points
ID: 21842289
Hi, I believe you're going to need a combination of the steps you've tried, plus the WaitOnReturn flag of the Run method.

BATCH1:
REM ===============
wscript VBS1.vbs
REM ===============

VBS1 (started by BATCH1):
'==================
strBatchFile = "BATCH2.bat"
Set objShell = CreateObject("WScript.Shell")
objShell.Run strBatchFile, 1, True
MsgBox "Finished"
'==================

BATCH2:
REM ===============
echo This is Test1.bat
start /wait wscript Test2.vbs
REM ===============

VBS2:
'==================
MsgBox "This is VBS2." & vbCrLf & "BATCH2 will wait for this to finish, because of the START /WAIT." & vbCrLf & "VBS1 will wait for BATCH2 to finish because of the True WaitOnReturn flag."
'==================

Regards,

Rob.
0
 

Author Comment

by:lynx7
ID: 21855067
hey Sirbounty and RobSampson,

thanks for your tips... for now it works for step 1 of my problem which i described above... (see the dir easy in the attached zip)

now the real situation is that batch2.bat is actually an exe file (made with autoIT), which is the batch2.bat file encrypted to run with admin rights.
as soon as i run this exe file the calling vbs1.vbs.doesn't wait anymore for the ending of the exe.
i tried "objShell.Run strExeFile, 1, True" and "'objShell.exec strExeFile" (as i read somewhere that this method automatically waits for the process to end...) but neither one works.
to test this, these files are located in the test dir in the attached zip.

hope you'll have a solution for this as well...

cheers,
S.


experts.zip
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 65

Expert Comment

by:RobSampson
ID: 21863025
Hi, when I ran your Batch2.exe it just exited straight away, so I wasn't sure what it was doing.

So, I took your VBS2.vbs file, and used PrimalScript professional to compile it to an EXE.  Then, when I renamed that to Batch2.exe and ran your "test" files, that behaved exactly the same way as the VBS version, and VBS1 waited for that EXE to finish.

So it must be that your exe actually launches another process.  While your EXE is running, does it run an executable with a specific name?  Perhaps you could set VBS up with this process monitor, just after you use the objShell.Run command.

Regards,

Rob.
strComputer = "."

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

boolStillRunning = True

Do While boolStillRunning = True

	Set colProcess = objWMIService.ExecQuery ("Select Name from Win32_Process WHERE Name='batch2.exe'")

	boolStillRunning = False

	For Each objProcess in colProcess

		boolStillRunning = True

	Next

	Wscript.Sleep 100

Loop

Open in new window

0
 

Accepted Solution

by:
lynx7 earned 0 total points
ID: 21863453
RobSampson,

when running the batch2.exe you have to make sure that the vbs2-exe.vbs file is in the same dir...
(since the exe is the compiled batch2.bat file which runs the vbs2.vbs)
(check out the _test.au3 file: RunAs($sUserName, $sDomain, $sPassword, 3, $sBatchFile) )

so i assume i'll have to monitor the running of the batch2.bat instead of the batch2.exe...

i do this (run bat which runs vbs) because when you directly start the vbs you get the wscript.echo as msgboxes instead of cmd line echo's...

hope this gives you a clearer view on the process..

in the meantime i'll try your monitor code.

cheers,
S.
0
 

Author Comment

by:lynx7
ID: 21864296
hey Robsampson,

while pasting the line "RunAs($sUserName, $sDomain, $sPassword, 3, $sBatchFile) " it suddenly struck me, why does this exe terminates so fast? because it does what it does (runas) and finishes of course ;-)...
so i found the runaswait method (haha!) and hoppa... the thing finally works, everything waits for everything as its supposed! i transposed it to my final printer driver installer/remover script and it just works fine!

super thanks for your time and useful input!
it made my day already!

cheers,
S.
0
 
LVL 15

Expert Comment

by:sr75
ID: 21878871
Lynx7,

You can just give the whole thing to RobSampson.  There is no reason to split up the points between his two posts.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now