So this bat file was running perfectly right before this latest windows update.
#Echo off
:Start
Start E:\directoryhere?listen -dedicated
echo Press Ctrl-C if you don't want to restart automatically
ping -n 10 localhost
goto Start
So this would start a dedicated server. A command prompt would pop up. When everyone left the server or the game finished, the command prompt would close then the .bat file would reopen it. Now after this update, the .bat file just keeps opening the cmd prompt while its open. So i'll have instantly 20 instances open at once and my cpu is at 100%.
I've also had this code before the windows update before this one which ended up doing the same thing.
#echo off
cd "E:\directoryhere\"
:loop
Start RxGame-Win64-Test.exe server lv_canyon?listen -dedicated | set /P "="
goto loop
That code used to work, but 2 window updates before ended up doing the same thing. It would just keep opening instances and make my cpu 100%.
What's a way to make sure to see if the cmd prompt is open and not to reopen it and keep it running until the cmd prompt closes then reopen it.
A simple fix to this can to check is the process already is open first using tasklist. Please make sure you search what your actual application is called. For this example I'm going to guess it's called RxGame-Win64-Test.exe. Bellow are a few script options.
This script bellow will check to see if the RxGame-Win64-Test.exe app is open first before starting another one:
#ECHO OFF
#SETLOCAL enabledelayedexpansion
GOTO LOOP
:LOOP
Rem | Check If Window Is Open
tasklist /FI "IMAGENAME eq RxGame-Win64-Test.exe" 2>NUL | find /I /N "RxGame-Win64-Test.exe">NUL
if not "%ERRORLEVEL%"=="0" (
Rem | Process Not Found
timeout /T 10 /NOBREAK
Rem | Restart Server
start "" "RxGame-Win64-Test.exe server lv_canyon?listen -dedicated"
Rem | GOTO LOOP
GOTO LOOP
)
GOTO LOOP
Not sure if the RxGame-Win64-Test.exe is CMD based program or not but if it is the script bellow will help you:
#ECHO OFF
#SETLOCAL enabledelayedexpansion
Rem | First Load, Start Server
start "DedicatedServerLauncher" cmd /c "RxGame-Win64-Test.exe server lv_canyon?listen -dedicated"
GOTO LOOP
:LOOP
Rem | Reset PID
Set "PID="
Rem | Grab The Current Window PID
FOR /F "tokens=2" %%# in ('tasklist /v ^| find "DedicatedServerLauncher" ^| find "Console"') do set PID=%%#
Rem | Check If Window Is Open
if "!PID!"=="" (
Rem | Process Not Found
timeout /T 10 /NOBREAK
Rem | Restart Server
start "DedicatedServerLauncher" cmd /c "RxGame-Win64-Test.exe server lv_canyon?listen -dedicated"
Rem | GOTO LOOP
GOTO LOOP
)
GOTO LOOP
For help on any of the commands do the following:
call /?
set /?
for /?
if /?
find /?
So on.
Related
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
I run a batch file which carries out some task for hours. BY mistake users happen to close the terminal window in which the batch file is executing by pressing the top right "X" close button.
I want to disable it and want to close the batch file window only by typing "exit" inside the window..is this possible?
This can't be done using only one batch file. But there might be a really dirty way for this. You will need three batch files:
EDIT: Here is the solution. Put these three files into the same folder.
controller.bat:
#ECHO OFF
TITLE CONTROLLER
IF NOT EXIST hiddenrunner.vbs (
echo CreateObject^("WScript.Shell"^).Run Wscript.Arguments^(0^), 0, False>hiddenrunner.vbs
)
tasklist /V|findstr "MONITOR"
IF %ERRORLEVEL%==1 hiddenrunner.vbs monitor.bat
:INPUTLOOP
CLS
SET /P input=Type 'exit' to exit:
IF /i NOT %input%==exit GOTO INPUTLOOP
ECHO ok, exiting
taskkill /F /FI "WindowTitle eq Administrator: WORK" /T
taskkill /F /FI "WindowTitle eq Administrator: MONITOR" /T
ping localhost -n 2
del /Q hiddenrunner.vbs
monitor.bat:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
TITLE MONITOR
hiddenrunner.vbs work.bat
:LOOP
tasklist /V|findstr "CONTROLLER"
IF !errorlevel!==1 START controller.bat
ping localhost -n 2
GOTO LOOP
work.bat:
#ECHO OFF
title WORK
ping -t localhost
In your case, the work-file will be your original batch you want to execute. You'll have to adjust the window titles (like TITLE CONTROLLER and WindowTitle eq Administrator: WORK) to fit your setup. However, this works for me. If the user closes the input console, it is being restarted after one second. All processes are being terminated after the user inputs exit in the input console.
I am wanting to run a batch file which builds visual studio project in another window and then return to original window and execute later commands.
but following command immediately prints LetterTwo without waiting for complete solution building
echo LetterOne
start /WAIT msbuild sim.sln
echo LetterTwo
According to the comments to the question, it seems, msbuild doesn't behave, like one would guess. So your only way is to "wait manually":
start "MyUglyApplication" msbuild sim.sln
:loop
timeout 1 >nul
tasklist /v | find "MyUglyApplication" && goto :loop
echo finished.
Give the new window a unique name, test if the process is running and if yes, continue testing.
i have able to solve the problem by modifying #stephan code a little bit
#echo off
start msbuild <solution>
:loop
timeout 1 >nul
tasklist /FI "IMAGENAME eq msbuild.exe" 2>NUL | find /I /N "msbuild.exe">NUL
if "%ERRORLEVEL%"=="0" goto loop
echo finished.
We're a little bit in panic, we're not experts and have been spending the day trying to find a line of code that would do what we need in our BAT file.
Basically, we have Windows 7 and we want that when our .bat file is run:
- The image is opened in the Image Viewer
- (In slideshow mode)
- And every time the BAT is run, the new image replaces the old one in the slideshow.
We have this configuration on a Windows XP and it works perfectly, with this line:
C:\WINDOWS\System32\rundll32.exe C:\Windows\System32\shimgbw.dll,ImageView_Fullscreen %~1
However, with Windows 7, impossible to make it work.
- The slideshow doesn't open.
- The next image is opened in a new Image Viewer window
We have tried to:
- Install the XP image viewer, using some tricks found on internet and modifying shimg.dll. Didn't work and now our system32 is quite messed up.
- Install IrfanView as an alternative. Didn't work
- Include "start" in the line, or "/b" to open in same window, but all this didn't really change anything... We're really not experts.
If you can provide any help or insight, that would be fantastic. Thank you.
This can be achieved by each batch file killing the previous one and thus replacing its image.
If you launch the image with the START command and give a task name; when the batch file launches it should use TASKKILL to delete the previous viewer by name and then START another one.
What program you use to display the image is up to you.
I have some batch subroutines I use, which can be used for this purpose, and I append them.
Put that Program name in serverbinary:
SET serverbinary=the.exe
CALL :GetPID ServerPID %servername% "%USERNAME% %serverbinary%"
CALL :KillPID %ServerPID%
START "%USERNAME% %serverbinary%" "%serverbinary%" %~1 < nul
EXIT /b
:: ------------------------------------------------------------------
:WaitForTask
::
:: Usage: Windowname imagename
:: Wait for a task to finish before continuing
::
set commentsPID=
CALL :GetPID commentsPID %1 %2
REM "%commentsPID%."
IF "%commentsPID%." == "." EXIT /b
echo Waiting for PID %commentsPID% %1 %2
CALL :Sleep 30
GOTO :WaitForTask
:: ===========================================================================
:GetPID
:: usage: PIDvar imagename windowname
:: Get the PID of a task
SETLOCAL EnableDelayedExpansion
set _SubArg=
for /f "skip=3 tokens=1,2*" %%a in ('tasklist /fi "imagename eq %~2" /fi "windowtitle eq %~3" 2^>nul ') do #set _SubArg=%%b
)
( ENDLOCAL
set %1=%_SubArg%
)
EXIT /B 0
:: ===========================================================================
:KillPID
:: usage: PID
SETLOCAL EnableDelayedExpansion
if NOT %1. == . taskkill /F /T /PID %1 > nul 2>nul
ENDLOCAL
EXIT /B 0
I have a bat file that I created, it adds keys to the windows registry and then calls another bat file, QGIS.bat (this bat file starts an application called QGIS).
It works most of the time but every now and then, when it calls QGIS.bat nothing happens, the command window stays open but QGIS (started by the QGIS.bat file) will not start.
In the command window(cmd) all it says is call .\usbgis\apps\qgis\bin\qgis.bat
(Just a note QGIS is a portable application that runs from a USB memory stick, might that be part of the problem?)
So my question. Is there a way you can terminate a bat file if it douse not close in 3 min or if the other bat file douse not start?
Thanks,
This is what I'm talking about in my comment:
#echo off
setlocal enabledelayedexpansion
set sPath=C:\Program Files\Internet Explorer
set sprog=iexplore.exe
set inc=0
:loop
if exist "%sPath%\%sProg%" (echo %sProg%) else exit /b
set /a inc+=1
if "!inc!" equ "30" (Echo Exiting & exit /b)
for /f %%a in (
'tasklist /NH /FI "Imagename eq %sProg%"^|findstr /i "INFO:"') do (
if not errorlevel 1 (
ping -n 11 127.0.0.1>nul
goto :loop
)
)
Obviously change the path and file to match yours. I just needed something for testing here.