Home > Back-end >  Make a batch file stop running after certain amount of time?
Make a batch file stop running after certain amount of time?

Time:03-04

I have a VBS script that calls a .bat file so it runs in hidden mode. The .bat file needs to run immediately at startup and keep waiting for a folder to be created so it kills an application then start it again. It works good, except i am not sure how to add a timer so that after 5 minutes it stops regardless of results. I tried adding timeout, but that didn't work. I also tried adding this to the batch file, but that didn't work. Any suggestions ?

for /l %%a in (1,1,48) do timeout 300 >nul

Here is the vbs script

Set oShell = CreateObject ("Wscript.Shell") 
Dim strArgs
strArgs = "cmd /c %systemdrive%\ExecuteCmd.bat"
oShell.Run strArgs, 0, false

and the bat file

@echo off
:search
if exist "%userprofile%\BlakFolder" (
    goto StopThisProgram
) else (
    goto search 
)

:StopThisProgram
taskkill /im BlerApp.exe /f 
goto ThisProgram

:StartThisProgram
TIMEOUT 10 /NOBREAK
START "" "C:\Program Files\BlerAp\BlerApp.exe"
Exit \b

CodePudding user response:

set /a timer=0
:search
if exist "%userprofile%\BlakFolder" goto StopThisProgram
timeout /t 1 >nul
set /a timer =1
if %timer% geq 300 goto stopthisprogram
goto search 

This will also remove the hard loop which will be eating CPU.

Every 1 second, timer is incremented. If it reaches 300, stop.

You may wish to reset timer to 0 within the start... block.

  • Related