Batch script - close Cmd.exe window and restart - windows

I have a batch file script which starts a program (bat) and after X seconds the batch script is close the program (exe) and restart it.
This is my code:
:loop
start "1.bat" "C:\1\1.bat"
Timeout 10
taskkill /f /im program.exe
goto loop
If the program is ending, I want that the Cmd.exe window closes, but only the "1.bat" window should be closed. I can't find a specified cmd window for my "1.bat" process in the task manager to close it.
If I close the programm.exe with && exit the batch doesn't restart!

You can do one of two things.
1 - If you have access to 1.bat (i.e. its not readonly or is not actually an .exe), then add exit after it runs the program.exe.
2 - Otherwise if you cannot modify 1.bat for whatever reason, you may need to add the following command line:
taskkill /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq 1.bat"
So your batch script will look as follows:
:loop
start "1.bat" "C:\1\1.bat"
Timeout 10
taskkill /f /im program.exe
taskkill /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq 1.bat"
goto loop
Or similar. Basically /FI is the filter switch and you can specify to filter based on the "IMAGENAME" (which equals "cmd.exe") and "WINDOWTITLE" (which equals "1.bat")

Related

How do you have a program close when another one is closed?

I'm trying to make it so that when I open a batch file, it will open 2 programs, and when the program Osu! closes, it will also close the program TabletDriverGUI The programs open, but I don't know how to close it.
Code:
start /d "C:\Users\ninja\AppData\Local\osu!" osu!.exe
start /min "TabletDriverGUI" "D:\EXE Files\Drivers\TabletDriverV0.2.3\TabletDriverV0.2.3\TabletDriverGUI.exe"
tasklist /V /FI "Osu! eq osu.exe*"
tasklist /V /FI "TabletDriverGUI eq TabletDriverGUI.exe*"
:loop
if "%ERRORLEVEL%"=="0" (
goto loop
) else (
taskkill /FI "TabletDriverGUI eq TabletDriverGUI.exe*"
)

How to close cmd in batch file

start cmd /k
/k: is compulsory which will execute.
launching many command propmts can be done as below.
start cmd /k Call rc_hub.bat 4444
start cmd /k Call rc_grid1.bat 5555
start cmd /k Call rc_grid1.bat 6666
start cmd /k Call rc_grid1.bat 5570.
I want to know what cmd to be used for closing after launching
Eg: if %a="1234" start cmd /k Call rc_grid1.bat --> This is opening cmd , what cmd needs to give to close ?
Closing multiple instances of cmd is not a very uncommon task.
It can arise, for example, when debugging some complex set of batch scripts.
There is one of the ways to do this:
First, give cmd windows unique titles. For example,
start "unique_title" cmd.exe /k ...
Second, when you want to close them, get process ids from tasklist output matching the titles. Then kill those ids with taskkill.
tasklist /v /fo csv | findstr "unique_title"
Here is the full example, the first argument is the title substring to match:
kill_title.cmd
#echo off
set TITLE=unique_title
if not "%~1"=="" set "TITLE=%~1"
for /f "tokens=1,2,* delims=," %%a in ('tasklist /fi "imagename eq cmd.exe" /v /fo csv ^| findstr "%TITLE%"') do (
rem here we check that the first column is "cmd.exe" ... just in case
if "%%~a"=="cmd.exe" echo taskkill /pid "%%~b"
)
First check the script output, and if it is ok, remove echo before taskkill.
When messing with command prompts, I kept crashing my computer when command prompts got REALLY out of control. I made another command prompt as follows:
Title END
:End
taskkill /FI "WINDOWTITLE ne END" /IM cmd.exe /F /T
Goto :End
This will force close all command prompts that are running, not including this one, and will repeat the command until you close it.

Windows TASKKILL syntax issue

After all these years, you'd think I had done some Windows batch files of some type. But you'd be wrong. I am updating a batch file and encountered the following statement:
TASKKILL /F /FI "WINDOWTITLE eq SIM RH*" /IM perl.exe
Why both the /FI and the /IM? Are the two clauses anded together? If I leave off the /IM, the correct processes get killed.
The only way this makes sense is if they get ended and kill only tasks with that window title AND are perl.exe tasks.
Yes they are added!
/IM would be killing all tasks with different process-IDs
/FI applies a filter to that. You can test that with something like this:
#echo off
for /L %%i in (1,1,5) do start "%%i" cmd.exe
timeout /t 3
taskkill /F /FI "WindowName ne 3" /IM cmd.exe
This should leave you with 1 command-prompt with the title 3. All others will be closed.
Changing the ne to eq would only close the command-prompt with the title 3 leaving you with 1, 2, 4, 5 and yourBatch.bat
Yes. The given /FI "WINDOWTITLE eq SIM RH*" option will filter processes by window title starting with SIM RH. The reference for taskkill specifies that if such a filter is given, the wildcard character * is accepted for the /IM option.
What isn't being said is the fact that adding a filter and leaving out the /IM seems to imply /IM *.
You can verify this behaviour with following command, which will open a regular shell and a powershell in two separate windows, and then kill both windows just by specifying a window title filter.
start "testp1" cmd && start "testp2" powershell && taskkill /F /FI "WINDOWTITLE eq testp*"

Restart terminal application with a batch file

I need a .bat file that will close and re-open start.cmd (C:\Users\Jake\Desktop\PocketMine-MP\start.cmd) <-- that's the file location. I need it to close and re-open every 75 min. The terminal has to close it can't stay open and launch another all I've gotten so far is:
#echo off
:loop
start "start.cmd" "C:\Users\Jake\Desktop\PocketMine-MP\start.cmd
timeout /t 20
taskkill /f /im "start.cmd" >nul
goto loop
Its starting the terminal every 20 seconds like I want it too but its not closing the old one.
If anyone can help it would assist me and my small network greatly.
Rename start.cmd to bat_start.cmd then try the following.
#echo off
:loop
start "bat_start.cmd" "C:\Users\Jake\Desktop\PocketMine-MP\bat_start.cmd"
timeout /t 20
taskkill /f /fi "windowtitle eq bat_start.cmd*" /im "cmd.exe" >nul
goto loop
If you use same file for control the loop and start file again, then it's fall in recursion. Following example work for me,
#echo off
:loop
start "test2" "C:\test2.bat"
timeout /t 60
taskkill /f /fi "windowtitle eq test2*" /im "cmd.exe" >nul
goto loop
test2 file
#echo off
echo "start job here"
:: Do your work here
pause :: remove the pause, it's just for simulating
exit
You can use the TimeCommander plugin. Set to run a restart command every 75 minutes.

Batch program to to check if process exists

I want a batch program, which will check if the process notepad.exe exists.
if notepad.exe exists, it will end the process,
else the batch program will close itself.
Here is what I've done:
#echo off
tasklist /fi "imagename eq notepad.exe" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit
But it doesn't work. What is the wrong in my code?
TASKLIST does not set errorlevel.
echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit
should do the job, since ":" should appear in TASKLIST output only if the task is NOT found, hence FIND will set the errorlevel to 0 for not found and 1 for found
Nevertheless,
taskkill /f /im "notepad.exe"
will kill a notepad task if it exists - it can do nothing if no notepad task exists, so you don't really need to test - unless there's something else you want to do...like perhaps
echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"&exit
which would appear to do as you ask - kill the notepad process if it exists, then exit - otherwise continue with the batch
This is a one line solution.
It will run taskkill only if the process is really running otherwise it will just info that it is not running.
tasklist | find /i "notepad.exe" && taskkill /im notepad.exe /F || echo process "notepad.exe" not running.
This is the output in case the process was running:
notepad.exe 1960 Console 0 112,260 K
SUCCESS: The process "notepad.exe" with PID 1960 has been terminated.
This is the output in case not running:
process "notepad.exe" not running.
TASKLIST doesn't set an exit code that you could check in a batch file. One workaround to checking the exit code could be parsing its standard output (which you are presently redirecting to NUL). Apparently, if the process is found, TASKLIST will display its details, which include the image name too. Therefore, you could just use FIND or FINDSTR to check if the TASKLIST's output contains the name you have specified in the request. Both FIND and FINDSTR set a non-null exit code if the search was unsuccessful. So, this would work:
#echo off
tasklist /fi "imagename eq notepad.exe" | find /i "notepad.exe" > nul
if not errorlevel 1 (taskkill /f /im "notepad.exe") else (
specific commands to perform if the process was not found
)
exit
There's also an alternative that doesn't involve TASKLIST at all. Unlike TASKLIST, TASKKILL does set an exit code. In particular, if it couldn't terminate a process because it simply didn't exist, it would set the exit code of 128. You could check for that code to perform your specific actions that you might need to perform in case the specified process didn't exist:
#echo off
taskkill /f /im "notepad.exe" > nul
if errorlevel 128 (
specific commands to perform if the process
was not terminated because it was not found
)
exit
That's why it's not working because you code something that is not right, that's why it always exit and the script executer will read it as not operable batch file that prevent it to exit and stop
so it must be
tasklist /fi "IMAGENAME eq Notepad.exe" 2>NUL | find /I /N "Notepad.exe">NUL
if "%ERRORLEVEL%"=="0" (
msg * Program is running
goto Exit
)
else if "%ERRORLEVEL%"=="1" (
msg * Program is not running
goto Exit
)
rather than
#echo off
tasklist /fi "imagename eq notepad.exe" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit
Try this:
#echo off
set run=
tasklist /fi "imagename eq notepad.exe" | find ":" > nul
if errorlevel 1 set run=yes
if "%run%"=="yes" echo notepad is running
if "%run%"=="" echo notepad is not running
pause

Resources