Auto close non responding , unknown or frozen Game - batch-file

My batch file is supposed to auto exit my game if it freezes or isn`t booting , which happens from time to time as it's finicky due to the RE Engine. Solution would be to have the game auto exit as soon as there's less than 10% Cpu usage or if its not responding or unknown status.
This is what I've got so far in my batch file:
#echo OFF
start "" "Autolaunch DMC5 aka Steam, required for Playing.ahk"
TIMEOUT /t 20
start "" "steam://rungameid/601150"
TIMEOUT /t 30
start /min "" "DMCVTrainer.exe"
start /min "" "SSSiyanCollabTU5.CT"
set "process_name=DevilMayCry5.exe"
::remove .exe suffix if there is
set pn=%process_name:.exe=%
setlocal enableDelayedExpansion
set c=0
:: getting three snapshots of CPU usage of the given process
for /f skip^=2^ tokens^=3^ delims^=^" %%p in ('typeperf "\Process(%pn%)\%% Processor Time" -sc 3') do (
set /a counter=counter+1
for /f "tokens=1,2 delims=." %%a in ("%%p") do set "process_snapshot_!counter!=%%a%%b"
)
:: remove rem to see the cpu usage from the three snapshots
rem set process_snapshot_
:: if all three snapshots are less than 0000010 process will be killed
if 1%process_snapshot_1% LSS 10000010 if 1%process_snapshot_2% LSS 10000010 if 1%process_snapshot_3% LSS 10000010 (
tskill %pn%
)
:RUNNING
tasklist|findstr DevilMayCry5.exe > nul
if errorlevel 1 (
timeout /t 1
taskkill /F /IM cheatengine-x86_64.exe
taskkill /F /IM DMCVTrainer.exe
taskkill /F /IM Steam.exe
) & GOTO ENDLOOP
timeout /t 1
GOTO RUNNING
:ENDLOOP
exit /B
wont let me update because my code isnt in proper format.

Related

How to set an expiration date on a batch file

I have a batch file with this code:
#ECHO off
START "" "C:\Program Files (x86)\Proxifier\Proxifier.exe"
timeout /t 5 >NUL
START "" "E:\Program Files (x86)\Epic Games\Launcher\Portal\Binaries\Win64\EpicGamesLauncher.exe"
timeout /t 65 >NUL
Taskkill /IM "Proxifier.exe" /F
Exit
I want to set an expiration date for this batch file.
For example: The programs wont run on 11/30/2018
How do I set an expiration date on this code?
Try this. Example dates are used in the IF conditions in a format YYYYMMDD (you can set your own values):
#echo off
for /f "tokens=* delims=" %%a in ('wmic os get LocalDateTime /value') do for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"
set "LocalDateTime=%LocalDateTime:~0,8%"
::echo %LocalDateTime%
:: EXPIRATION DATE ::
set "EXP_DATE=20181130"
:::::::::::::::::::::
if %LocalDateTime% GTR %EXP_DATE% (
echo this wont work anymore
exit /b
)
START "" "C:\Program Files (x86)\Proxifier\Proxifier.exe"
timeout /t 5 >NUL
START "" "E:\Program Files (x86)\Epic
Games\Launcher\Portal\Binaries\Win64\EpicGamesLauncher.exe"
timeout /t 65 >NUL
Taskkill /IM "Proxifier.exe" /F
Exit
It is very simple:
#echo off
set "expirationDate=30112018"
call:check_expire
if %var_return% EQU 0 (goto main_code) else (exit /b)
goto main_code
:main_code
START "" "C:\Program Files (x86)\Proxifier\Proxifier.exe"
timeout /t 5 >NUL
START "" "E:\Program Files (x86)\EpicGames\Launcher\Portal\Binaries\Win64\EpicGamesLauncher.exe"
timeout /t 65 >NUL
Taskkill /IM "Proxifier.exe" /F
pause
exit /b 0
:check_expire
for %%a in (%date%) do set dt=%%a
For /f "tokens=1-3 delims=/ " %%a in ('echo %dt%') do (
if "%expirationDate%" LEQ "%%a%%b%%c" (set "var_return=1") else (set "var_return=0")
)
Please check also https://social.technet.microsoft.com/Forums/scriptcenter/en-US/14804d47-5887-4119-a9fd-167d2a14df53/set-a-batch-file-expiration-date?forum=ITCG
Hope this helps!

Create a batch file where it will open eg: 50 processes of program X- and if total of program X doesn't match 50 (eg it crashed) to have it reopen

So I'm currently trying to make a batch file where if program X will open 50 times - and if the amount of program X does not match 50 - eg it crashed etc, then it will reopen till it is once again 50.
Currently I have
:test
start /high /WAIT /D c:\test\ c:\test\Server.exe -file "c:\test\config\test.txt"
echo (%time%) WARNING: x closed or crashed
echo (%time%) Moving logs
for /f "tokens=1-5 delims=/ " %%a in ('echo %date%') do set dateX=%%d-%%b-%%c
for /f "tokens=1-5 delims=:." %%a in ('echo %time%') do set timeX=%%a-%%b-%%c
mkdir "C:\test2 - test2\%dateX% %timeX%\"
move "%APPDATA%\test\log.txt" "C:\Users\Administrator\Desktop\test2\test2\%dateX% %timeX%\log.txt"
echo (%time%) Logs moved. Restarting server...
ping localhost -n 10 > nul
goto :test
which basically has the reopen if crashed part + move text file over - but I just need the open/check 50 times part.
Any help will be appreciated -
Thanks!
EDITED - to adapt to comments. Added delay between started instances of program
setlocal enableextensions enabledelayedexpansion
rem what to open
set "_process=notepad.exe"
set "_keep=4"
rem infinite loop: from 0 to 1 in steps of 0
for /l %%a in (0 0 1) do (
rem get count of started processes
for /f %%b in ('tasklist ^| find /c /i "%_process%"') do if %%b LSS %_keep% (
rem start as many processes as needed
set /a _started=%%b+1
for /l %%c in (!_started! 1 %_keep%) do (
start "" "%_process%"
set /a _wait=(%%c-1)*2
timeout /t %_wait% > nul
)
)
rem wait
timeout /t 3 > nul
)

Combination of `sleep` and `pause` command

Ok, after seeing crazy stuff being completed in so little code, I have high hopes this is possible.
Pretty much, I want to use the pause command normally, however, if the user doesn't input anything for a specified duration of time, it automatically continues.
In pseudo code:
(sleep %sleep-time%&Echo Pass)1>0 & pause
I thought at first I could do this using start /b to create a process that echoed input while being paused i the current thread, but that could cause problems if the user does input something.
Bonus
What would be really cool is if the errorlevel would be changed based on whether the user inputted something, or if the pause command timed out.
I suggest using timeout:
timeout /T 60 >NUL
This will sleep your script for 1 minute, or unless the user hits a key.
#echo off
setlocal
rem TimedPause.bat - Antonio Perez Ayala
if "%1" equ ":PausePart" goto PausePart
if "%1" neq "" goto begin
echo TimedPause.bat seconds
echo/
echo Wait for given seconds or until user press a key
echo At end, the presence of keyPressed.txt file indicate the cause of exit
goto :EOF
:begin
set seconds=%1
start "" /B "%~F0" :PausePart
for /F "skip=2 tokens=2 delims=," %%a in ('tasklist /FI "IMAGENAME eq cmd.exe" /FO CSV') do (
set PausePart=%%a
goto TimePart
)
:TimePart
ping -n 2 localhost > NUL
if exist keyPressed.txt goto :EOF
set /A seconds-=1
if %seconds% gtr 0 goto TimePart
taskkill /PID %PausePart% /F > NUL
goto :EOF
:PausePart
del keyPressed.txt 2> NUL
pause
echo %time% > keyPressed.txt
exit

batch file to check if exe is running if so taskkill

I'm having problems with the firefox Flashplayerplugin eating up too much ram and lagging my system when it's not in use. The only solution I found was killing the flashplayerplugin while using firefox, uninstalling, reinstalling or a fresh firefox install or new profile doesn't solve it; however, it's becoming very tedious having to check taskmanager all the time and kill it and the flashplayerplugin always seems to start on it's own.
The question I have is if it's possible to create a batch file to check if FlashPlugin_11_8_800_94.exe is running and kill it after a period of time (5-10 seconds) and continue running the batch file actively, in a loop, scanning if FlashPlugin_11_8_800_94.exe has started again, then kill it after 5 - 10 seconds, rinse and repeat?
Edit:
Found a batch file and modified it, but also seems to be missing some perimeters to actively search if it's running, even when it is not. It doesn't work either way though.
#echo off
:search
TASKLIST|FIND "FlashPlayerPlugin"
IF %ERRORLEVEL% = 0 THEN (GOTO found)
TIMEOUT /T 5
GOTO search
:found
taskkill /im FlashPlayerPlugin_11_8_800_94.exe
--
This batch file doesn't work either.
set tasklist=%windir%\System32\tasklist.exe
set taskkill=%windir%\System32\taskkill.exe
-------------------------------------------------------
:STOPPROC
set wasStopped=0
set procFound=0
set notFound_result=ERROR:
set procName=%1
for /f "usebackq" %%A in (`%taskkill% /IM %procName%`) do (
if NOT %%A==%notFound_result% (set procFound=1)
)
if %procFound%==0 (
echo The process was not running.
goto :EOF
)
set wasStopped=1
set ignore_result=INFO:
:CHECKDEAD
"%windir%\system32\timeout.exe" 3 /NOBREAK
for /f "usebackq" %%A in (`%tasklist% /nh /fi "imagename eq %procName%"`) do (
if not %%A==%ignore_result% (goto :CHECKDEAD)
)
goto :EOF
-------------------------------------------------------
:MAIN
call :STOPPROC FlashPlayerPlugin_11_8_800_94.exe
taskkill /im FlashPlugin_11_8_800_94* /f >nul 2>&1
For anyone who still might find it useful:
This is a small script that scans tasklist for processes containing processname once every 5 seconds. For example, if you put "notepad" for the processname, it will end processes like "notepad.exe" and "notepad++.exe". To use the script, copy and paste the following into notepad and save has "simple_pk.cmd". processname can have any characters except double quotes("), ampersands (&), or commas (,).
::Simple monitor and kill process
#echo off&prompt :&mode con cols=50 lines=10
set processname=flashplayerplugin
:loop
cls&echo Searching for %processname%...
for /f "tokens=1 delims=," %%a in ('tasklist /fo csv ^|FINDSTR /I /C:"%processname%"') do call :killprocess %%a
ping -n 6 127.0.0.1>NUL
goto :loop
:killprocess
echo. |set /p d=killing %*...
taskkill /f /im "%*">nul 2>&1
set err=%errorlevel%
set success=Success
if not %err%==0 set success=fail (err code: %err%)
if %err%==128 set success=fail (process not found)
echo %success%&goto :eof
This is a slightly different version of the same script. This will only end processes that match the whole name exactly:
::Simple monitor and kill process (exact name)
#echo off&prompt :&mode con cols=50 lines=10
set processname=FlashPlayerPlugin_11_8_800_94.exe
:loop
cls&echo Searching for %processname%...
for /f "tokens=1 delims=," %%a in ('tasklist /fo csv ^|FINDSTR /C:"%processname%"') do call :killprocess %%a
ping -n 6 127.0.0.1>NUL
goto :loop
:killprocess
set name=%*
set name=.,;%name:"=%;,.
echo %name%|FINDSTR /C:".,;%processname%;,.">nul || goto :eof
echo. |set /p d=killing %*...
taskkill /f /im "%*">nul 2>&1
set err=%errorlevel%
set success=Success
if not %err%==0 set success=fail (err code: %err%)
if %err%==128 set success=fail (process not found)
echo %success%&goto :eof
Your IF is not correct:
#echo off
:search
TASKLIST|FIND "setup.exe"
IF %ERRORLEVEL% equ 0 (GOTO found)
TIMEOUT /T 5
GOTO search
:found
taskkill /im setup.exe
More simpler form
#echo off
:search
TASKLIST|FIND "setup.exe"
IF %ERRORLEVEL% equ 0 (taskkill /im setup.exe
exit)
TIMEOUT /T 5
GOTO search
I was looking over the first bit of code at top and I find a way, I believe. This is what I got; really simple.
#echo off
cls
:start
timeout /t 5
tasklist|find "explorer.exe"
goto found
goto start
:found
taskkill /f /im explorer.exe
goto start
Of course, any program will work.

Batch Loop with Timer

Hi I'm new to batch and I am running into a issue. What i'm looking to do is to write a batch file that will run a command test.exe over and over for 24 hours. Once the 24 hour timer expire, the program will exit.
I am not familiar with batch and was wondering how this can be done.
Try this code below. Using ping as timer and hiding it in the background using find switches.
You can modify timer by playing with ping command and to exit play with %n% variable.
This code re-runs test approx every 30 minutes and runs only 4 times
#Echo off
SET n=0
:Loop
SET /A n=n+1
test.exe
Ping 1.1.1.1 -n 1000 -w 1 | find /V "Request timed out" | find /V "Ping" | find /V "Packets"
if %n% EQU 4 (
exit
) Else if %n% LEQ 3 (
Goto Loop
)
Use a label in your batch file to create a loop. Inside the loop, execute the test.exe.
For the 24-hour execution period, I believe you can create a scheduled task to start and stop the execution. I am not sure how graceful the exit will be once the scheduled task will end.
Dunno if a double else is feasable... never tried... but here's my shot.
#Echo off
FOR /F "tokens=1" %%F IN ('ECHO %date%') DO (
set day=%%F
)
FOR /F "tokens=* delims=:." %%F IN ('ECHO %time%') DO (
set thetime=%%F
)
:Loop
START /B "test.exe"
FOR /F "tokens=1" %%F IN ('ECHO %date%') DO IF %date%==%day% IF %time% GEQ %thetime% (
GOTO Loop
) Else (
IF NOT "%date%"=="%day%" IF %time% LEQ %thetime% (
GOTO Loop
) ELSE (
IF NOT "%date%"=="%day%" IF %time% GRT %thetime% (
GOTO:EOF
)
)

Resources