Batch file, run next program if already running - batch-file

Basically I have a simple batch script just now that starts a program multiple times let's call it 1.exe, 1.exe will launch 20 times and then be killed after % amount of seconds (specified by a command line argument).
What I need done is if 1.exe is already running, launch 2.exe instead, if 2.exe and 1.exe are already running launch 3.exe instead and so on.
All the code I really have just now is
Timeout /t 20 /nobreak >nul
echo.
taskkill /F /IM %programname%
echo.
pause >nul
and also the launching of the 1.exe
Any help is appreciated.

The Batch code below launch 2.exe if 1.exe is already running, launch 3.exe if 2.exe is already running and so on:
for /L %%i in (1,1,20) do (
for /F "skip=3" %%e in ('tasklist /fi "imagename eq %%i.exe"') do (
if "%%e" equ "%%i.exe" set lastExe=%%i
)
)
set /A lastExe+=1
%lastExe%.exe

Related

Bat script to run commands in a running cmd

I use a windows .bat script to start all of my game servers
The script looks like this:
cd "C:\Users\Administrator\Desktop"
start Minecraft_HUB
start Minecraft_LOBBY
start Minecraft_SURVIVAL
with many more servers
It works for launching all of them at the same time but I need to manually type stop / exit in every single one to shut them down.
Is there any way I could have a .bat script that would insert and run a save & shut down command in a running cmd console?
I don't yet know how to make that script
From the prompt:
for /f "tokens=1,2delims=," %b in ('tasklist /fo csv') do echo %~b|find /i "minecraft">nul&if not errorlevel 1 echo taskkill /pid %~c
As a batch-file line:
for /f "tokens=1,2delims=," %%b in ('tasklist /fo csv') do echo %%~b|find /i "minecraft">nul&if not errorlevel 1 echo taskkill /pid %%~c
Note that the taskkill command will merely be echoed to the console. Remove the echo before the taskkill to actually execute the taskkill
Untested. Use at your own risk.

Turning Off a Program When the other one closes .Bat file

This is my code so far ive tested it and both programs open when .bat is run , What im after is the code to close program 2 when program 1 closes any help would be appriciated as i have no coding knowledge :(
#echo off
start "TestFolder" "C:\Program Files (x86)\testfolder\test1.exe" %1
start "Testfolder2" "C:\Program Files (x86)\testfolder2\test2.exe"
taskkill /f /im test2.exe
exit
You need to setup a FOR /F Loop to monitor the output of TaskList to check for the window title of the 2nd program (and you'll need to explicitly set the window title of that program to something unique to do that.)
Then you will need to use Taskkill to kill the other task.
#(SETLOCAL
echo off )
CALL :Main
( ENDLOCAL
EXIT /B )
:Main
start "TestFolder1" "C:\Program Files (x86)\testfolder\test1.exe" %1
start "Testfolder2" "C:\Program Files (x86)\testfolder2\test2.exe"
CALL :Loop
taskkill /f /im test2.exe
GOTO :EOF
:loop
SET /A "n=10"
TIMEOUT 10
tasklist /V /NH /FI "IMAGENAME eq test1.exe" | FIND /I "TestFolder1" &&(
GOTO :loop
)
GOTO :EOF

How to Kill a Command after certain period of time in Command Prompt even if it is running state

I am running a command in command prompt and I want to kill it after 15 minutes. I don't care if it is completed or not. I don't want to have any human interaction to kill the running command such as someone has to press CTRL+C to kill it.
Is there a way to do it.
Please note I don;t want to use any third party tools or scripts.
start forfiles /s
timeout /t 5
Taskkill /im forfiles.exe /f
is one way.
Did you mean something like that ?
#echo off
Tracert www.google.com
timeout /t 900 /nobreak >NUL
Taskkill /im cmd.exe /f
Edit : based on the blueray's comment :
how can I put this command in a single line to use in CMD?
Tracert www.google.com & timeout /t 900 /nobreak >NUL & Taskkill /im cmd.exe /f
May be this could help
start your-command-here
ping 127.0.0.1 -n 120 > NUL
taskkill /im cmd.exe /f
Explanation:
start: this command starts executing your command
ping: it pings your local machine(ip : 127.0.0.1) for 120 sec and >NUL redirects the output to nowhere else the output of ping command will be displayed on the cmd screen
taskkill: it is used to kill any task
/im: image name of the process to be terminated. if the command is running on cmd then cmd.exe or any program that you need to kill.
Hope it helps.
Try this, works well for me:
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
#echo off
set _time=0
set _timeout=30
start myprocess.exe
:waitforprocess
set /A _time=_time+1
IF !_time! GTR !_timeout! goto TimedOut
rem TaskList will return the task list of the image as specific
rem or it will return INFO: No tasks are running.... so look for
rem the INFO statement using FindStr. FindStr will return a errorlevel
rem of 0 if it found the string and a 1 if it did not, so use that
rem to work out next steps.
rem -------------------------------------------------------------------
tasklist /NH /FI "IMAGENAME EQ myprocess.exe" | findstr INFO
rem ERRORLEVEL 1 = Did not find the string, so try again.
IF ERRORLEVEL 1 (
timeout /T 1 /NOBREAK > nul
goto :waitforprocess
)
GOTO DONE
:TimedOut
ECHO We timedout so will kill the process
taskkill /FI "IMAGENAME EQ myprocess.exe" /T /F
:Done
echo finished

I want to run and kill a process with same batch file

I have bitlord I want this program to run when i click a batch file and when i don't want this program to run i want to kill it with same batch file. Can anyone please write the script Thanks a lot for giving your time.
I tried it scripting but failed.
If bitlord is not running then this will launch it
If bitlord is running then this will kill it
Change line 2 and line 3 to set the program name and path.
#echo off
set "program=bitlord.exe"
set "folder=c:\program files\bitlord"
tasklist |find /i "%program%" >nul || start "" "%folder%\%program%" & goto :EOF
tasklist |find /i "%program%" >nul && taskkill /f /im "%program%" & goto :EOF
Okay. I think i understand your question now:
#echo off
taskkill.exe /f /im notepad.exe
start calc.exe
The magic is to use the start command which starts a new command asynchronous. Meaning, it doesn't wait for the termination.

How to start an .exe file using input/output file and set a time limit in batch?

In windows command prompt, when I want to run a program using input/output file, I always use batch command like the following: test.exe < input.in > output.out.
(test.exe is the name of program, input.it is the name of input file and output.out is the name of output file)
But if I use this command, I cannot set a time limit for that program (i.e. I cannot force the program to quit after an amount of time).
So what command I should use in order to do that? Thank you for helping me.
You can use taskkill command:
start test.exe
ping 127.0.0.1 -n 5
taskkill /im test.exe /f
Here it's killed after 5 seconds. You can specify any duration in seconds.
start "" /b cmd /c "test.exe <input.in >output.out"
timeout /t 10
tasklist | find "test.exe" >nul && taskkill /f /im test.exe
Start the program inside a cmd instance not attached to the current console, wait for 10 seconds and if program still in task list, kill it
EDIT - Updated to handle the case pointed in comments
#echo off
setlocal enableextensions
start "" /b cmd /c "test.exe <input.in >output.out"
call :timeoutProcess "test.exe" 10
if errorlevel 1 (
echo Program has been killed
) else (
echo Program has ended in time
)
exit /b
:timeoutProcess process timeout
for /l %%t in (1 1 %~2) do (
timeout.exe /t 1 >nul
tasklist | find "%~1" >nul || exit /b 0
)
taskkill /f /im "%~1" >nul
exit /b 1

Resources