batch file label running for 30 seconds - batch-file

I program to both learn how and to make fun files for jokes and tricks. I'm trying to make a batch file that will run a label inside the batch file for a set amount of time like 30 seconds without having to use a for-do statement. what I have so far is shown below and is reduced to a small test, but uses a for-do statement.
# echo off
for /l %%a in (1,1,10) do (
call :matrix)
echo Thanks for using the MATRIX
pause
:matrix
echo %random%%random%

use timeout command, it can set amount of time like 30 seconds without having to use a for-do statement.
# echo off
for /l %%a in (1,1,10) do (
call :matrix)
echo Thanks for using the MATRIX
timeout 30
:matrix
echo %random%%random%
if you don't want to show count done message, you can use timeout 30 >nul

this starts a second cmd process (minimized), that simply does a timeout 30. It's existence is the signal to repeat the loop.
#echo off
start /min "MyTimer" timeout 30
:loop
echo %random%%random%
tasklist /fi "windowtitle eq MyTimer" | find "Console" >nul && goto :loop
Sadly, tasklist doesn't give useful errorlevels, so we have to use find.
Note: for debugging purposes you may want to remove the start switch /min.

Related

How to make a .bat file check for a program and relaunch if it isn't open, also restarting the program if it's been running for x amount of time?

This can be two separate .bat files if needed, but I would prefer if it's possible on one.
I need to run a certain program, but it sometimes crashes.
I have this for a .bat so far.
It works as far as resetting the program every hour, but it does crash sometimes in between restarts.
I'd like to just have a check, so it launches if the program isn't found.
Is that possible?
#echo off
:loop
start "xx" "xxpath"
timeout /t 3600 >null
taskkill /f /im "xx" >null
timeout /t 4 >null
goto loop
Here is a sample batch that can check if any instance of chrome.exe is running or not
If not, we start it !
#echo off
Color 0A
Set WaitTimeSeconds=20
Set App_Path=C:\Program Files\Google\Chrome\Application\chrome.exe
for %%i in (%App_Path%) do set App_Name=%%~nxi
Title Checking if any "%App_Name%" instance is running ...
:Loop
Rem Clear the screen
cls
Rem Kill any application that have a status equal to not responding !
Taskkill /f /fi "status eq not responding">nul 2>&1
Rem Check if any application instance is running ; if not we start it !
TASKLIST | FINDSTR %App_Name% || START "" "%App_Path%"
Timeout /t %WaitTimeSeconds% /nobreak>nul
Goto Loop

End a command after a given time in a bat file

I have a bat file to execute several programs, and there is a possibility that one program stays in a loop. And I would like to kill the execution of that program after 1 min and execute the next one.
The programs I would like to execute gif_0.exe, gif_1.exe, ... receiving inputs from txt and writing the output to another txt.
gif_0.exe input1.txt output1_0.txt
timeout /t 20
gif_0.exe input2.txt output2_0.txt
timeout /t 20
gif_1.exe input3.txt output3_0.txt
timeout /t 20
gif_2.exe input4.txt output4_0.txt
timeout /t 20
gif_3.exe input5.txt output5_0.txt
My idea is similar to Geisterfurz007.
But my batch starts the exe in parallel and also another instance of the batch with the name of the started exe as an arg. The new instance checks the arg and jumps to the sub where it waits for the timeout and tries to kill the exe.
#echo off
setlocal EnableDelayedExpansion
Set Wait=60
If "%1" Neq "" Goto :KillTask
for /l %%n in (0, 1, n) do (
set /a foo=%%n+1
start gif_%%n.exe input!foo!.txt output!foo!_0.txt
Start %~f0 gif_%%n.exe
)
Goto :Eof
:KillTask %1
timeout /t %Wait% /nobreak>nul
taskkill /F /IM %1
start "Title goes here" gif_0.exe input1.txt output1_0.txt
timeout /t 60 /nobreak>nul
taskkill /F /IM gif_0.exe
Should do the trick. Repeat after your needs.
Explanation:
starts your application with the given parameters
waits 60 seconds without the option to stop the timer before counting down and without any output.
Kills the task with the imagename of your application. If you may have several instances of this paralelly running this will kill all of them!
You can use filters to narrow down on one though; have a look at taskkill /?
I am not sure if there is a typo in your question but assuming your applications are gif_n.exe with the parameters inputn+1.exe outputn+1.exe
You can do the following:
#echo off
setlocal EnableDelayedExpansion
for /l %%n in (0, 1, n) do (
set /a foo=%%n+1
start gif_%%n.exe input!foo!.txt output!foo!_0.txt
timeout /t 60 /nobreak>nul
taskkill /F /IM gif_%%n.exe
)
This would go from 0 to n in single steps and would execute the same thing like above in a loop.
2 additions here:
The line setlocal EnableDelayedExpansion is needed to activate the possibility to use variables values after they were changed within a closed block of parenthesis like a for-loop or an if-condition.
set /a foo=%%n+1 will set the value of variable foo to the value of %%n+1. The switch /a is needed to perform an actual calculation and not the appending of a string.
To use the variable in our for-loop we have to use the DelayedExpasion (see above):
Simply change the % you would usually use to ! and you are done.
To make sure, everything works correctly you might want to place an echo in front of your three main lines.
Feel free to ask questions if something is unclear :)

how to minimize the popup of command prompt when batch file is running in loop?

#ECHO OFF
C:
for /L %%a in (1,0,10) do (
echo This is iteration %%a
start "" "C:\Desktop\fast\JumpTracker.exe" -COM7
TIMEOUT 1
)
When I run this code it's running after 1 second interval and also pop a window of command prompt. The batch file is running in a (for loop) , I want to minimize the command prompt but batch file should be in running condition. Is it possible? can any one help me?
As explained by #bgoldst:
Does start /b work to prevent the creation of the popup window?
Therefore the result should be:
#ECHO OFF
C:
for /L %%a in (1,0,10) do (
echo This is iteration %%a
start /b "" "C:\Desktop\fast\JumpTracker.exe" -COM7
TIMEOUT 1
)

How to start a batch file when a text file is created?

I am trying to work out how to run a batch file when a text file is created. I have tried to research but am unable to find anything that helps.
The Pseudocode of what I want is something like this:
When "Response.txt" is created run "2 Week.bat".
Thanks.
#echo off
:start_loop
if exist "c:\Response.txt" (
start cmd /c "c:\2weeks.bat"
goto :exodus
)
rem :: wait for 1 minute
w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:60 >nul 2>&1
goto :start_loop
:exodus
This will work on Vista and later:
#echo off
:loop
if exist "c:\folder\Response.txt" "c:\folder\2 week.bat"
rem - reduce CPU usage by waiting for 15 seconds
timeout /t 15 /nobreak
goto :loop

How to set a timeout for a process under Windows 7?

I would like to start a program with a windows batch file. But the program should stop after a certain timeout value. For example: Run the program 60 seconds and stop it after 60 seconds.
Under Linux, there is this nice timeout command to do what I want. Windows has also a timeout command, but its just to pause a command, to delay its execution. Is there something else under Windows to do so ?
Setup: Windows 7, 64 Bit, Professional
start yourprogram.exe
timeout /t 60
taskkill /im yourprogram.exe /f
Bali C gave a concise and to the point answer.
I needed something a little more featureful and reusable.
Based on Bali C's Example. I came up with this.
If anyone should need the same as me.
your.bat
REM...
CALL STARTwaitKILL..bat /relative/path/your_program.exe
REM...
STARTwaitKILL.BAT
#ECHO OFF
IF[%1]==[] GOTO EOF
IF NOT EXIST %1 GOTO EOF
REM SET PRIORITY=/NORMAL
REM ADJUST PRIORITY, BELOWNORMAL LETS BATCH FILE RUN MORE SMOOTHLY
REM WITH A PROGRAM THAT CONSUMES MORE CPU. SEE ABOUT MAXWAIT BELLOW
SET PRIORITY=/BELOWNORMAL
REM SET PRIORITY=/LOW
REM 0 NORMAL WINDOW :: 1 NO WINDOW :: 2 MINIMIZED WINDOW
SET /A HIDDEN=1
REM MAXWAIT HERE IS MORE LIKE MINIMUM WAIT IN WINDOWS.
SET MAXWAIT=10
SET WAITCOUNT=0
SET ARGS=/I %PRIORITY%
IF %HIDDEN% EQU 1 SET ARGS=%ARGS% /B
IF %HIDDEN% EQU 2 SET ARGS=%ARGS% /MIN
START %ARGS% %1
:WAIT
IF %WAITCOUNT% GEQ %MAXWAIT% GOTO KILL_IT
TIMEOUT /T 1 > NUL
SET /A WAITCOUNT+=1
FOR /F "delims=" %%a IN ('TASKLIST ^| FIND /C "%~nx1"') DO IF %%a EQU 0 GOTO RUN_DONE
GOTO WAIT
:KILL_IT
TASKKILL /IM %~nx1 /F > NUL
:RUN_DONE
Could be fleshed out ore to take more arguments for priority and such, but I don't have the need for it. Shouldn't be hard to add.
Don't exist any command in Windows to delay an app or to set a timeout for an app
Timeout in Windows is for Delay the execution process of CMD/Batfile, nothing more utility.
You can use external tools for that, I don't remember the name of any now, so many underground software, sorry, but I remember that in the autoit official forum exists a similar commandline tool to launch an app setting the timeout,
and maybe in the tool NIRCMD, or ps2exec, check their help files, or someone inside the WAIK Kits.
This is the only you can do:
#Echo OFF
:: First launch the app in background mode, because some applications stops the execution of CMD.
Start /B ".\Dir\Your app.exe"
:: Then stay in background for a certain time
Timeout /T "Seconds"
:: Continue your code...
Pause&Exit
The start+timeout+taskkill waits exactly the given time. Since I needed to stop waiting if the process exits earlier, I created my own solution in C++.
The tuxliketimeout program mimics the GNU timeout. Feel free to download&compile from
https://github.com/cernoch/tuxliketimeout
In windows 10 the easiest way is with scriptrunner:
Demonstrate the timeout by running a pause command (this will kill the called process):
ScriptRunner.exe -appvscript cmd "/c" "pause" -appvscriptrunnerparameters -wait -timeout=20

Resources