Bat File to close Program after 5 minutes of inactivity - batch-file

First off, some background: I'm a student on co-op at an engineering firm, and I need to write a .bat file to close a scanning program after 5 minutes of not being use. the scanner is on a local network, and won't let anyone open the program to scan anything if someone else is using it.
So my question is whether or not there is a .bat command that can check inactivity, like measure how long it's been since someone has clicked a button, and after 5 minutes of nothing, close the program.
I've made a file that will open the program, and close it after a 5 minute timer, which will suffice for now, but I want to try to make it better. Any advice would be great! Thanks!
#echo off
cd "C:\WINDOWS\twain_32\escndv"
start escndv.exe >NUL
PING 1.1.1.1 -n 1 -w 300000 >NUL
Taskkill /im escndv.exe >NUL
echo Epson GT-20000 has been terminated due to inactivity
PAUSE
I have minimal experience with .bat files, so what I have is from a couple hours looking around on Google.

You could try something like this:
schtasks /create /sc ONIDLE /i 5 /tn scanner /tr closescanner.bat
This will create a scheduled task to run closescanner.bat when the pc has been idle for 5 minutes.
Just create a separate batch file called closescanner.bat with Taskkill /im escndv.exe >NUL inside to close the app when no-one has touched the pc for 5mins.
Hope this helps!

Related

How to start a .exe after a specific game closes with a batch file

I have just did this code with a batch file that closes the windows process explorer.exe. It starts some other programs that help with stuttering and closing explorer.exe helps quite a bit.
Thing is to start the game, I need to open a launcher but I want to close the launcher when I start the game. The game is fortnite. In the code it ends explorer.exe but when I close fortnite (Not the launcher), I want explorer.exe to start up.
Code:
START C:\"Program Files (x86)"\"Epic Games"\Launcher\Portal\Binaries\Win32\EpicGamesLauncher.exe
START C:\Users\Yqmxh\Documents\Optimization(s)\TimerResolution.exe
START C:\Users\Yqmxh\Documents\Optimization(s)\"Mz Game Accelerator.lnk"
Taskkill /F /IM explorer.exe
start /wait "C:\Windows\explorer.exe"
exit
I don't know how much cpu time this consumes, but it would work at least:
:beginning
tasklist | FIND "FortniteClient-Win64"
IF NOT %ERRORLEVEL% == 1 (ECHO process was found
ECHO now waiting for six seconds
PING localhost -n 6 >NUL
GOTO beginning
)
ECHO the EXE is not running anymore
explorer
Since I considered the FOR loop too complex, I settled with a GOTO, which I hope I won't get downvoted for ;)
I think it's pretty self-explaining, but feel free to ask, if I'm wrong.

Task Schedule Screensaver and end after 2 hours

I have a small batch which I would like to improve,
"C:\Program Files (x86)\ACD Systems\ACDSee Pro\4.0\D3DScreenSaver\Acdsee.scr" /s
timeout /t 7200
taskkill /im "C:\Program Files (x86)\ACD Systems\ACDSee Pro\4.0\D3DScreenSaver\Acdsee.scr" /f
this runs the screen saver and after 2 hours end's it as the screen is off by that time.. I dont want the cpu to be active and HDD to continue displaying photos.
the timer starts after the screensaver ends!! :(
Can we somehow make the batch better, my issues are
when I move the mouse screensaver stops. which is what I want,
but the timeout command prompt is visible and I force it closed everytime, can this be automatic with auto detection of the app being closed / ended
thankyou
Try this:
start "" "C:\Program Files (x86)\ACD Systems\ACDSee Pro\4.0\D3DScreenSaver\Acdsee.scr" /s
timeout /t 7200
taskkill /IM /F "Acdsee.scr"

How to kill a service in a batch file every 20 seconds?

A task called "FireSvc.exe" (McAffee service) keeps interfering with our app upgrades. I put
> taskkill /f /im "FireSvc.exe"
in a batch file. We continuously run this during installs so the software will successfully upgrade. I'm not positive why this service starts back so often. We have to run from the command line, because in the task manager you get "access denied" when trying to kill this task.
My question is, how would you make this run every 20-30 seconds?
We cannot install any type of non-approved software either. So, theres that...
Thanks for any input.
Here's what we use:
:runagain
taskkill /f /im "FireSvc.exe"
timeout /T 5
goto runagain
You can use vbscript's sleep command in a batch file with cscript like so...
First create a vbscript file (.vbs extension) that contains the following code (don't forget to save it with ANSI encoding otherwise it won't work):
Wscript.sleep 2500
Wscript.exit
Create a batch file in the same directory with this code:
#echo off
:Kill
cscript //NoLogo file.vbs
taskkill /f /im "FireSvc.exe"
GoTo Kill
I currently don't have access to a PC to check if it works so let me know what happens. I still think there might be a cleverer alternative... cheers!
Edit: Btw you can also simulate a sleep command with the ping command like so:
ping localhost -n 1 -w 2500 >nul
And if you are using windows vista or above you can use the timeout command.

how to end a program after a predetermined set of time (in minutes) as a .bat file

I want to build a simple .bat file that after a predetermined set of time (like an hour or so) it will kill a program once's it's started. So say I run the batch file and after 1 hour or so it will kill another program (say firefox)
dr_andonuts has a standard way or practice I observe many do
An alternative is
batchfile content:
timeout 3600
taskkill /f /im firefox.exe
To make the batch execution pause, you can use a kludge as described in this answer. The 3600 corresponds to 3600 seconds, or one hour.
ping 127.0.0.1 -n 3600 > nul
To kill firefox, you can add a command such as
taskkill /im firefox.exe

How to automatically close App_A when I close App_B using batchfile

Hi everyone I'm a newbie in batchfiling but loved tinkering and coding every other time. Can I make a batch file that closes two program simultaneously? Example, I created a batchfile that opens two program, App_A (gamepad imulator) is minimized App_B (offline RPG Game) normal window. What I want is when I close App_B App_A would automatically close too so that I don't have to restore the window and manually close the imulator.
this is the code I just did which I also found in this site and got it working:
ECHO OFF
start /d "C:\Documents and Settings\Computer\My Documents\Imulator\PROFILE" SET1.imulatorprofile /m
ECHO IMULATOR STARTED
start /d "C:\Program Files\App_B" App_BLauncher.exe
ECHO APP_B STARTED
ECHO OPERATION COMPLETE
Any comments or suggestions is GREATLY APPRECIATED! THANKS in ADVANCE :)
You can use another batch file with two taskkill lines, one for each of your apps, and launch that.
Otherwise you'd need to have a batch file running all the time in a window, which loops and checks if appB is not running and then it will close appA. It's not very elegant.
I'm not very good using the windows commandline, but I would try the following approach:
start imulator (which should quit automatically after APP_B exited)
start APP_B (using the /wait option - this should pause the batch processing)
kill imulator (using PsKill by name)
You can find details about start, PsKill and other commands at this site.
Hope that helps a bit.
*Jost
...added later...
Another option would be to do regular checks in the background if a process (App_B) is running and continue (with stopping App_A) when it is finished. This approach makes sense when App_B is only a launcher for another process (e.g. App_Launched_By_B) and comes back directly.
This can be done with a small loop which might look similar to this one:
start "App_A" /d "C:\Programs\App_A" App_A.exe
ECHO App_A STARTED
start "App_B" /d "C:\Programs\App_B" App_B.exe
ECHO App_B STARTED
ECHO GIVE App_B 30 SECONDS TO LAUNCH App_Launched_By_B
SLEEP 30
:LOOP
PSLIST App_Launched_By_B >nul 2>&1
IF ERRORLEVEL 1 (
GOTO CONTINUE
) ELSE (
ECHO App_Launched_By_B IS STILL RUNNING - PAUSE ANOTHER 5 SECS
SLEEP 5
GOTO LOOP
)
:CONTINUE
PsKill App_A
ECHO App_A STOPPED
This example came originally from another answer and was written by aphoria. I adapted it just a little little bit for this case.
Additional information about PsList and other commands can be found at this site.
I also want to note that I do not really like this approach, because it consumes some cpu without doing much. But it is the only solution from a batch file I can see.

Resources