cheapkid1
cheapkid1

Reputation: 469

batch file that opens a program waits 5 seconds then closes that program

For some reason I cannot get this batch to perform the open portion, wait, then the close portion in Windows XP or Windows 7. I can only open or close by commenting out the other in the batch, but for some reason how I have it written it will only open. How can I open a program wait a fixed time, then close that same program? The reason for doing this is to force a manufacturer program's drivers when the computer starts. Any help would be much appreciated. Thanks.

@ECHO off
"C:\Program Files\WinTV\WinTV2K.EXE" -nc
Set _Delay=5
Taskkill /IM "WinTV2K.EXE" /F
Exit

Upvotes: 2

Views: 33043

Answers (5)

user9150112
user9150112

Reputation:

This will open your program wait 5 sec., then close that same program timeout for Windows XP - 10 "batch file" or "Windows Command Script"! no ping!!!

@ECHO off
start WinTV2K.EXE -nc
ver | find "XP" > nul
     if %ERRORLEVEL% == 1 timeout /t 05 && echo . Vista - 10 && goto NEXT

set XP-timeout=5000
echo . XP, 1000 = 1 sec, approximately

set /a timeout=1
     :timer
echo timeout timer number %timeout%
if %timeout% == %XP-timeout% goto NEXT
echo . XP, 1000 = 1 sec.
set /a timeout=timeout+1
goto timer
     :NEXT
tasklist /fi "imagename eq WinTV2K.EXE" |find ":" > nul
 if errorlevel 1 taskkill /f /im "WinTV2K.EXE"
Exit

Upvotes: 1

foxidrive
foxidrive

Reputation: 41234

Instead of

Set _Delay=5

use this

ping -n 6 localhost >nul

The ping command requires 6 to give you an approximate delay of 5 seconds.

Upvotes: 8

cheapkid1
cheapkid1

Reputation: 469

Thanks guys for your help with the wait ping, that was a great fix for the timer, however the only way I could get the batch to work was to place the batch file inside the program directory and run it like this:

@ECHO off
start WinTV2K.EXE -nc
ping 1.1.1.1 -n 1 -w 5000 > nul
Taskkill /IM WinTV2K.EXE /F
Exit

Not sure why it wouldn't work the other way, and it's not exactly what I wanted, but it will do. Thanks for all your help. @Ryan and @Sean Long

Upvotes: 3

Sean Long
Sean Long

Reputation: 2301

You could reference this for further information, but my favorite is to ping a non-existent machine for a period of time:

ping 192.0.2.2 -n 1 -w 10000 > nul

Changing 10000 for what you need (it's in milliseconds).

Upvotes: 2

Ryan
Ryan

Reputation: 3582

Instead of Set _Delay=5, use the following:

ping 1.1.1.1 -n 1 -w 5000 > nul

Will ping 1.1.1.1 once (-n 1) and wait 5 seconds for a response (-w 5000)

As the value is outputted to nul it won't affect your program other than waiting for 5 seconds.

Upvotes: 1

Related Questions