# Batch help: Start program and wait until it's closed or I close it.



## Ketasmurf (Jun 17, 2009)

Hello folks. I need some help with a batch script i'm trying to create.

I need it to start a program and then wait 10 min to continue the script. But if i choose to close the program after 5 min. It also have to continue the script.

Any bright ideas would be greatly appreciated 


- Ketasmurf


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

Here's the basic steps:

Start the program
Set TotalWait time to 0
Start waiting
Use ping (or a sleep utility) to wait X seconds.
Add X to the TotalWait time.
If TotalWait time is 10 minutes (600 seconds or 600,000 milliseconds) continue.
If not 10 minutes yet, use tasklist to see if the program is still running.
If it's running go back to Start waiting

HTH

Jerry


----------



## Ketasmurf (Jun 17, 2009)

Hello and thanks for your help.

Is it possible for you to make a small example for me ? Because I'm not sure how to use the "Set TotalWait time" ?

I hope I'm not asking to much


----------



## TheOutcaste (Aug 8, 2007)

This will start Notepad and wait 10 minutes for it to exit before continuing.
If Notepad exits before then, it will continue within _ChkInt Seconds, 10 Seconds in this example.

```
@Echo Off
:: Set Check Interval and MaxWait times in seconds
Set _ChkInt=10
Set _MaxWait=600
:: Specify Program to start, extension is required
Set _ProgName=Notepad.exe
:: If the Image Name displayed in Task Manager is different
:: than the program name, specify it separately here
Set _ImgName=%_ProgName%
:: This makes sure name does not include a path
Call :_FixName "%_ImgName%"
Set /A _PWait=_ChkInt*1000
Set _TotalWait=0
Start "" "%_ProgName%"
:_WaitLoop
Ping 1.0.0.0 -n 1 -w %_PWait%>Nul
Set /a _TotalWait+=_ChkInt
If %_TotalWait% GEQ %_MaxWait% Goto _Continue
tasklist /NH /FI "IMAGENAME eq %_ImgName%" 2>Nul|Find /I "%_ImgName%">Nul
If %ERRORLEVEL%==0 Goto _WaitLoop
:_Continue
:: Place code to run when script continues here
Echo Waited %_TotalWait% Seconds Or %_ProgName% Exited
Pause
Goto :EOF
:: Subroute to strip path if one is present
:_FixName
Set _ImgName=%~nx1
Goto :EOF
```
HTH

Jerry


----------



## Ketasmurf (Jun 17, 2009)

Thanks. I almost got it working. But i can't get the time right. With the code you just posted it just continues the script after 10 sec.

How do i fix that ? 

Thanks again.


----------



## TheOutcaste (Aug 8, 2007)

That could happen if the "program.exe" you use to start the program is not the same as the actual "imagename.exe" that appears in task manager.
Start Task Manager (right click the task bar, click Task Manager), goto the Process tab, then start the program and see what name gets added to the list of running Processes.
If it's different, you'll need to edit the *Set _ImgName=* line to match the name that appears in Task Manager instead of getting set to *%_ProgName%*


----------



## Ketasmurf (Jun 17, 2009)

I just try to open notepad.exe like in your example.

And in task mannager it is also just called notepad.exe


----------



## TheOutcaste (Aug 8, 2007)

Odd, if you didn't change anything in the Posted code, it should open Notepad and then wait until you close Notepad, or 10 minutes have elapsed.

I just copied the above code to double check and it's running fine on My PC. Closed Notepad after about 2 minutes, and it says it waited 150 seconds.

One thing, if you add code after the end of the above, you'll need to add a *Goto :EOF* line so the subroutine returns properly and doesn't fall through into anything added after the end of the above code. I'll edit the above to add that.


----------



## Ketasmurf (Jun 17, 2009)

Humm... Weird. I just can't get it to work. When i run the complete above script witout any changes on XP it waits 10 sec. and then it says "Waited 10 seconds or notepad.exe Exited"

And when i run it on Vista it waits 10 sec. and then says "Waited 600 seconds or notepad.exe Exited"

Any ideas ?


----------



## TheOutcaste (Aug 8, 2007)

I don't have a clue. Here's a screen shot from mine, ran it three times.
1st two I exited Notepad, the last one you can see Notepad is still open.

I'll give it a try in Vista, see if it does anything different.


----------



## Ketasmurf (Jun 17, 2009)

This is how it looks when i try to run it on XP. I really dont get it. It always just continues after 10 sec.


----------



## TheOutcaste (Aug 8, 2007)

Ah, it's not an English version of Windows. I suspect the language difference it the problem.

With Notepad running, type this into a command prompt, and let's see what it returns:
*tasklist /FI "IMAGENAME eq not*"*


----------



## Ketasmurf (Jun 17, 2009)

On Danish XP:
When I type in tasklist /FI "IMAGENAME eq not*" with notepad running it just says Error: Search Filter not recognised.

Tested the script on a English vista. There it works fine. So must be because of the language. But how do i solve it so it will work on Danish OS. It would be best if i could work on both danish and English OS. 

Thx....


----------



## TheOutcaste (Aug 8, 2007)

OK, I'm guessing either *IMAGENAME* or the *eq* part needs to be in Danish.
I'll need you to get the help for the *Tasklist* program so I can see what it needs to be, and a couple of registry values. Then we can decide on the best way to see what language it's running under

Open a Command Prompt on the Danish system
Type *tasklist /?* and press *Enter*
Type the following two lines, and press *Enter* after each

```
reg query HKLM\SYSTEM\CurrentControlSet\Control\Nls\Language\ /v InstallLanguage
reg query HKLM\SYSTEM\CurrentControlSet\Control\Nls\Locale /v (Default)
```
(You can copy and paste the lines: Highlight the line, press *CTRL+C* to copy, then in the Command Prompt, right click the window and click *Paste*)
Right click in the Command Prompt window and click *Select All*
(alternate - Right click the Title bar, point to *Edit*, click *Paste* or *Select All*)
This will highlight the entire window
Press *Enter*. This copies everything to the clipboard
Switch back to this thread, right click in the Reply window, then click *Paste*.

Jerry


----------

