Simplest way to wait for command to finish before continuing

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Simplest way to wait for command to finish before continuing

#1 Post by MKANET » 13 Oct 2012 20:07

How can I execute the below line; and, wait until it exits memory before continuing?

Code: Select all

cmdow /run /hid C:\PROGRA~1\INTERN~1\iexplore.exe http://movies.netflix.com/WiHome


I can do this successfully without using cmdow (below). I just can't figure out how to include the full command line above; which also includes cmdow.

Code: Select all

start /b /wait cmd /C "C:\PROGRA~1\INTERN~1\iexplore.exe http://movies.netflix.com/WiHome"


Thanks in advance,
Last edited by MKANET on 13 Oct 2012 21:26, edited 1 time in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Simplest way to wait for command to finish before contin

#2 Post by abc0502 » 13 Oct 2012 20:37

try writing at this way:

Code: Select all

start /b /wait cmd /C "C:\PROGRA~1\INTERN~1\iexplore.exe" "http://movies.netflix.com/WiHome"

The IE between double quotes and also the url

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Simplest way to wait for command to finish before contin

#3 Post by MKANET » 13 Oct 2012 21:24

Huh? I dont get it. Your example doesnt work. Plus, it doesn't include cmdow. Maybe I didn't explain well enough?


abc0502 wrote:try writing at this way:

Code: Select all

start /b /wait cmd /C "C:\PROGRA~1\INTERN~1\iexplore.exe" "http://movies.netflix.com/WiHome"

The IE between double quotes and also the url

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Simplest way to wait for command to finish before contin

#4 Post by Ed Dyreen » 13 Oct 2012 22:54

MKANET wrote:

Code: Select all

start /b /wait cmd /C "C:\PROGRA~1\INTERN~1\iexplore.exe http://movies.netflix.com/WiHome"
Using /b with /wait doesn't makes sense, the script can't continiue cause it didn't start in parallel /b.
The /b option starts other scripts inside the current cmd session, *.EXE files don't start in a cmd session.
start expects the first argument to be the title.

The correct syntax for the start command would be something along the lines of:

Code: Select all

if /i !extension!==CMD (
       start "!title!" !startArgs! /D "!fullPath!" "!comspec!" /k ""!fileName!.!extension!" !args!"
) else start "!title!" !startArgs! "!file!" !args!
This may help:
Script to search google - problem with multiple quotations
Re: Search Internet

mka-temp
Posts: 2
Joined: 13 Oct 2012 23:28

Re: Simplest way to wait for command to finish before contin

#5 Post by mka-temp » 13 Oct 2012 23:57

Thanks for the quick reply Ed! I lost access to my primary login, so, using a temporary login for now.

Unfortunately, I have no idea what to make of the example you put. I know at least that it's not something I can copy and paste. :) For example, what are the !titles! for? Are they even necessary? I'm not sure what comspec means and don' t know what to put in it's place. Why are the extentions and filenames all separated?

Ideally, it would be much easier if I had a working example. Generally, I can learn and understand working examples much better if at all possible. I apologize in advance if I'm asking too much; as, I dont know the complexity involved.

Thanks for any further help you (or anyone else can offer).
-MKANET

Ed Dyreen wrote:
MKANET wrote:

Code: Select all

start /b /wait cmd /C "C:\PROGRA~1\INTERN~1\iexplore.exe http://movies.netflix.com/WiHome"
Using /b with /wait doesn't makes sense, the script can't continiue cause it didn't start in parallel /b.
The /b option starts other scripts inside the current cmd session, *.EXE files don't start in a cmd session.
start expects the first argument to be the title.

The correct syntax for the start command would be something along the lines of:

Code: Select all

if /i !extension!==CMD (
       start "!title!" !startArgs! /D "!fullPath!" "!comspec!" /k ""!fileName!.!extension!" !args!"
) else start "!title!" !startArgs! "!file!" !args!
This may help:
Script to search google - problem with multiple quotations
Re: Search Internet

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Simplest way to wait for command to finish before contin

#6 Post by abc0502 » 14 Oct 2012 00:07

@MKANET, what Ed means that the start command take this form:
start "cmd title" "switches and commands"

the title Ed pointed at is the cmd window title, if you didn't set the title or don't want to you must add two empty double quotes because if you didn't the first commands will be considered as a window title and then it won't work

did i explained that well :?: :!:

and about this code:

Code: Select all

if /i !extension!==CMD (
       start "!title!" !startArgs! /D "!fullPath!" "!comspec!" /k ""!fileName!.!extension!" !args!"
) else start "!title!" !startArgs! "!file!" !args!


Ed said that exe applications can't start in a cmd window (don't run in the cmd window) not like the batch files (.cmd or .bat).

so you may want to check for the extension of the file/program you are about to run.
if it is a exe file it will run the "Else" part, and if it is a cmd it runs the first if/statement part.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Simplest way to wait for command to finish before contin

#7 Post by Ed Dyreen » 14 Oct 2012 02:16

abc0502 wrote:Ed said that exe applications can't start in a cmd window (don't run in the cmd window) not like the batch files (.cmd or .bat).
Sorry, didn't formulate that well. exe's can start from a cmd shell, but it's not required here.

Code: Select all

start "" /wait iexplore.exe "http://movies.netflix.com/WiHome"
The empty quotes are not required here, but if you don't start may fail.
How can I execute the below line; and, wait until it exits memory before continuing?

Code: Select all

start "" /wait "c:\my path\cmdow.exe" /run /hid iexplore.exe "http://movies.netflix.com/WiHome"
But now the problem is that cmdow has no /wait option so it will complete and make the start /wait option to finish too.

Code: Select all

@echo off

cmdow.exe /run iexplore.exe "http://movies.netflix.com/WiHome"

:winWait
ping 0.0.0.0 >nul
tasklist /nh /fi "windowTitle eq netflix*" 2>nul |findStr.EXE /bric:"iexplore.EXE" >nul ||goto :winwait

:winWaitClose
ping 0.0.0.0 >nul
tasklist /nh /fi "windowTitle eq netflix*" 2>nul |findStr.EXE /bric:"iexplore.EXE" >nul &&goto :winwaitClose

pause
exit
But now the problem is that it will hang when you run /hid because there won't be a winTitle.

Code: Select all

cmdow.exe /run /hid iexplore.exe "http://movies.netflix.com/WiHome"

:winWait
ping 0.0.0.0 >nul
tasklist /nh /fi "imageName eq iExplore.EXE" >nul ||goto :winwait

:winWaitClose
ping 0.0.0.0 >nul
tasklist /nh /fi "imageName eq iExplore.EXE" |findStr.EXE /bric:"iexplore.EXE" >nul &&goto :winwaitClose

pause
exit
Finally it works, with the only limitation only one instance of iExplore can be active simultaneously.

What I don't understand is why you want to run iExplore.EXE hidden, you won't be able to interface with it and you would need taskill to terminate it :? :?:

I would consider a more capable language for whatever it is you are trying to achieve.

Code: Select all

WinSetState( $winHandle, '', @SW_HIDE )
WinWaitClose( $winHandle, '' ) ; Also works for hidden windows.

mka-temp
Posts: 2
Joined: 13 Oct 2012 23:28

Re: Simplest way to wait for command to finish before contin

#8 Post by mka-temp » 14 Oct 2012 13:43

Thanks guys. I probably should have explain what I'm trying to achieve; and, what I've tried already.

This is part of a bigger batch file to make Internet explorer maximize, then restore it's position to avoid a cosmetic bug when forcing it to run without a addressbar, menubar, etc; leaving only the window frame. I wanted to hide it while it's being maximized and unhide it after it's been restored to its windowed position.

I've actually already tried banging away against tasklist using a similar method (not as effective as yours though) before I even posted here. It was actually working.

Code: Select all

:waittofinish
tasklist /FI "IMAGENAME eq iexplore.exe"| find /I /N "iexplore.exe" >nul
if not errorlevel 1 goto waittofinish


But, discarded that idea due to it wasting too much CPU cycles. Especially, since it has to constantly do this during the duration of a movie; which is a very CPU sensitive task. I had even tried adding a delay in the loop. But, the whole idea didn't sound appealing to me. Which is what brought me to this forum... I was really hoping for someone to figure out a clever way of using "Start /wait"; if at all possible; which doesn't waste any CPU cycles. I thought if there was any way to do this, someone where would be able to figure out how.

The whole purpose of this batch file it to run Internet Explorer in a windowed-kiosk mode for Netflix. This would have been so much easier if there wasn't a cosmetic bug when enabling NoNavBar=1 on my system. Maximizing and restoring the window displays it correctly. I wanted the batch file to hide it while it's maximized and unhide it after it's restored to it's position.

I thought about doing this in autoit; especially since I could use existing code which hides, unhides, activates, minimize, maximizes windows using WinSetState($title, "", @SW_HIDE), WinSetState($title, "", @SW_SHOW), etc; but I preferred to do this in a batch file since I'm more comfortable using batch files; even if there's a little help from external command line apps.

If there's absolutely no way to do this, I'll have to settle for using tasklist with something like a 5 second delay in the loop.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Simplest way to wait for command to finish before contin

#9 Post by Ed Dyreen » 14 Oct 2012 15:38

mka-temp wrote:I was really hoping for someone to figure out a clever way of using "Start /wait"; if at all possible; which doesn't waste any CPU cycles. I thought if there was any way to do this, someone where would be able to figure out how.
There is nothing wrong with start /wait, as it nicely waits for cmdow /run to finish. This doesn't help, cmdow exits immediately after it has spawned the process.
mka-temp wrote:I thought about doing this in autoit; especially since I could use existing code which hides, unhides, activates, minimize, maximizes windows using WinSetState($title, "", @SW_HIDE), WinSetState($title, "", @SW_SHOW), etc; but I preferred to do this in a batch file since I'm more comfortable using batch files; even if there's a little help from external command line apps.

If there's absolutely no way to do this, I'll have to settle for using tasklist with something like a 5 second delay in the loop.
You could add the .au3 code inside your batch and run from there.

Code: Select all

; @goto :main &Enable au3 script comment, must be first block in batch !
; (
   #cs
; )

: main
:: (
       @echo off &prompt $G

       echo.My batch script commands here:

       pause
       exit

       copy "%~nx0" "%~n0.au3"
       start "%~n0" /wait "%~n0.au3"

       pause
       exit
:: )
exit /b 0

; Disable au3 script comment, must be last block in batch !
; (
   #ce
; )

; My au3 script commands here:
; main_()
; (
       WinSetState( $winHandle, '', @SW_HIDE )
       WinWaitClose( $winHandle, '' ) ; Also works for hidden windows.
; )

Post Reply