2

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
cheapkid1
  • 469
  • 7
  • 18
  • 32
  • Try `TIMEOUT /T 5` instead of `Set _Delay=5` – Ryan Feb 26 '14 at 15:55
  • I tried TIMEOUT instead of Set _Delay and I get:" 'TIMEOUT' is not recognized as an internal or external command, operable program or batch file. " – cheapkid1 Feb 26 '14 at 16:11

5 Answers5

8

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.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
3

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

cheapkid1
  • 469
  • 7
  • 18
  • 32
  • Was it opening the program in a new window without using the start call? If it wasn't, then the batch would have to wait for the program to finish before it could move on to any new task. – Sean Long Feb 27 '14 at 14:48
  • It was opening a cmd window, but wouldn't run the next line until the program called in "" was manually closed. – cheapkid1 Feb 27 '14 at 15:16
2

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).

Community
  • 1
  • 1
Sean Long
  • 2,163
  • 9
  • 30
  • 49
1

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.

Ryan
  • 3,552
  • 1
  • 22
  • 39
  • For some reason the ping doesn't start until I manually close the program but leave the batch cmd prompt window open, then the ping starts. So it's like it's getting stuck after opening the program command. – cheapkid1 Feb 26 '14 at 16:54
  • It doesn't continue to the next command line until the "C:\Program Files\WinTV\WinTV2K.EXE" -nc line has closed or discontinued. I tried opening a manual instance of the program and re-running the batch then manually closing the "batch opened program" and it closed the manual running program as I suspected. – cheapkid1 Feb 26 '14 at 17:22
1

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