Shut down machine after being idle with batch file? - windows

I don't even know if this is possible, but is there anyway to shut down a machine after (n) minutes, using a batch file?
Currently I already have a batch file which works alongside ranorex and a virtual machine, and I need the machine to shut itself down after it has been idle for 10 minutes or so, just to give everything else in the batch file plenty of time to run. Is there any way of doing this?

Just use the command for shutdown and add the option for the time to wait:
shutdown /s /t 600 /f
/s is for shutting down the computer
/t is for wait before do the operation with time in seconds (10*60=600)
/f is for forcefully close all applications
With shutdown /a you can abort the action befor the time runs off. For more options use the help of the shutdown-command.

Related

How to silently run a batch file every 5 minutes

A Windows 10 application starts to lag every 10-15 minutes so every once in a while, when I notice the lag, I run the below batch file to delete the temp files. It works to eliminate the application lag but I would like to automate the batch file so that once I initiate it, it loops every 5 minutes without the cmd window poping up. Since I dont know how long I will be using the application and dont want the batch file runing after I finish using the application I need to be able to terminate the batch file manually.
Appreciate to let me know if there is options with and without the windows task scheduler.
#echo off
set targetdir=C:\ExampleDir
del /q %targetdir%\*
for /d %%x in (%targetdir%\*) do #rd /s /q ^"%%x^"

Windows shutdown timer combined with abort timer

Because I'm lazy I made these very simple batch files
shutdown /s /t 2500
and
shutdown /a
If you are familiar with the Windows command line the first one just sets a timer after which the computer shuts down and the second one aborts this timer.
However they are two separate files. My question is: how to combine them into one? How to make 'if' statement which checks if turn off timer is engaged and aborts it and if it isn't engaged it sets the timer? Just for extra laziness I want to make it one click start/stop timer.
Also if there is a way, can I bind a batch file to specific key combination?
Mark the fact that the computer is being shut down by creating a temporary file.
If the file exists, then abort and delete the file.
#echo off
set SDF=%TEMP%\shutdown
if exist %SDF% (echo aborting shutdown
shutdown /a
del %SDF%
) else ( echo shutdown initiated
shutdown /s /t 2500
echo XX>%SDF%
)
pause

Automatically Batch File if still running after 5 minutes i.e. hanging

I have a BAT File which connects to an FTP site to send/receive files. On rare occasions the batch file will hang if there is an issue with the network. I would like to know if there is a command to automatically exit the batch file if it has been running longer than 5 minutes? This would be placed in the batch file I assume, a kind of countdown from when it first opens.
Thanks.
Brett
You have to have two windows.
start ftp etc
Timeout /t 300
taskkill /im ftp.exe /f
This will kill all ftp.exe not just one. NOTE this relys on the fact we are running ftp without running cmd in the second console window.

Batch file won`t start program when launched from windows service

I have created a batch file to run when a specific service stops.
The batch file should stop the relevant software running, restart some services (stop / start) and start the software again.
It`s a simple code:
#echo off
Taskkill /F /IM program1.exe
Taskkill /F /IM program2.exe
timeout /t 5
net stop service1
net stop service2 && net start service2
net start service3
timeout /t 2
start C:/path/program1.exe
start C:/path/program2.exe
Have set the service up to run the batch file as recovery at first failure.
Batch file is linked via a shortcut to be able to run it as administrator.
This works perfectly when running the batch file directly, but when it`s executed by the service recovery, the start-up of the software fails.
Does anyone have any idea what could be wrong?
In some circumstances timeout /t 5 might cause trouble. Try to replace it with PING -n 6 127.0.0.1 > NUL.

Batch file to commit changes to Embedded XP and then restart pc

I would like to create a batch file to run a cmd command to commit changes to a windows embedded pc from a USB drive and restart the PC to make the changes active.
The cmd line I use is:
ewfmgr -commit c:
But I need to open the cmd prompt and run the command then once it has run, restart the PC
This is what worked
#echo OFF
:reboot
c:\windows\system32\ewfmgr.exe C: -commit
echo Rebooting...Please Wait
c:\windows\system32\xpepm.exe -restart
pause
shutdown /? could give some hints. Then your batch file might look as follows:
ewfmgr -commit c:
shutdown /r
To ensure batch wait until ewfmgr command ends, use
start "" /W ewfmgr -commit c:
shutdown /r
With the /W or /WAIT switch, the start command will start application and wait for it to terminate. More info on start command.
To give a some kind of wait after the commit command so that it can run and finish its task, e.g. for a delay of 30 seconds:
add timeout /T 30 /nobreak>nul line before shutdown /r, and/or
use shutdown with /t xxx switch (this sets the time-out period
before shutdown to xxx seconds), i.e. shutdown /r /t 30
A workaround if timeout command is not recognized: PING -n 31 127.0.0.1>nul
Create a new file with the .bat extension. Open it in your preferred text editor and enter the commands you want to be run, and save the file. The commands that JosefZ wrote would probably do the job perfectly.

Resources