At the moment, I am using a Word/Caffeine batch to avoid falling asleep, but when I open multiple documents, several instances of Caffeine appear and it becomes bothersome to close them manually.
I am wondering if there is a batch that can keep my computer from sleeping while Word or any other program is running, but will allow it to revert to the default sleep setting once the program is closed.
Sometimes, we need to work on our computer without interruptions such as sleep mode, but manually preventing sleep can be tedious. This is where batch files come in handy. Batch files are scripts that automate tasks on a computer. In this blog post, we will discuss how to create a batch file that prevents sleep when a specific program is running and reverts to default sleep mode when the program is closed. We will use Word as an example, but this method can be applied to any program.
Creating the Batch File
To create a batch file, open Notepad and type the following command:
@echo off :loop if exist "C:Program FilesMicrosoft OfficerootOffice16WINWORD.EXE" ( powercfg -change -standby-timeout-ac 0 ) else ( powercfg -change -standby-timeout-ac 20 ) timeout /t 5 goto loop
This script prevents sleep mode when Word is running by setting the standby timeout to 0 (meaning the computer will not go to sleep) and reverts to default settings (20 minutes) when Word is closed. The script checks if Word is running every 5 seconds and continuously loops until the script is terminated.
Customizing the Batch File
To customize the batch file for a different program, replace “WINWORD.EXE” with the name of the executable file of the desired program. You can find the name of the executable file by opening Task Manager, right-clicking on the program, and selecting “Open file location.” The path to the executable file will be displayed in the address bar.
You can also customize the standby timeout by changing the value after “-standby-timeout-ac.” The value is in seconds, so 0 means no timeout and any other value will set the timeout to that number of seconds.
Saving and Running the Batch File
After customizing the batch file, save it with a .bat extension (e.g. preventsleep.bat). To run the batch file, simply double-click on it. The script will run in the background and prevent sleep when the specified program is running.
To stop the script, open Task Manager, go to the “Details” tab, find the batch file (e.g. preventsleep.bat), right-click on it, and select “End task.” Alternatively, you can open Command Prompt and type “taskkill /im preventsleep.bat /f” to terminate the script.
This method only prevents sleep when the specified program is running. If you switch to another program or leave your computer idle, the default sleep settings will apply. Additionally, this method only works on Windows computers. Mac and Linux computers have different commands for preventing sleep.
Batch files can be a useful tool for automating tasks on a computer. In this blog post, we discussed how to create a batch file that prevents sleep when a specific program is running and reverts to default sleep mode when the program is closed. This method can be customized for any program and can save time and frustration in manually preventing sleep. However, it’s important to note the limitations of this method and to always be aware of your computer’s power settings.
For Windows 10 64-bit, here’s how to create a one-line shortcut or cmd batch file to modify power settings (display timeout, sleep timeout, hard disk timeout, etc.), launch any program, and restore the original power settings when the program is closed.
The power timeouts range from 1 minute to 9999999 minutes, and the most common AC/DC power settings are provided. Instead of a batch file, a Windows shortcut can be used.
A default sleep timeout of 30 minutes is preferred, and the shortcut can be initiated in a minimized state. The path to the program should be adjusted accordingly. The shortcut can be executed with or without admin privileges.
It is not recommended to launch the program with this syntax: start “” “C:\Program Files (x86)\Microsoft Office\OFFICE11\WINWORD.EXE.lnk”. This applies to desktop computers.
A picture demonstrating the minimized state is provided at the bottom:
C:\Windows\System32\cmd.exe /c POWERCFG /CHANGE /STANDBY-TIMEOUT-AC 1440 &"C:\Program Files (x86)\Microsoft Office\OFFICE11\WINWORD.EXE" &POWERCFG /CHANGE /STANDBY-TIMEOUT-AC 30
On a laptop both AC and DC:
C:\Windows\System32\cmd.exe /c POWERCFG /CHANGE /STANDBY-TIMEOUT-AC 1440 &POWERCFG /CHANGE /STANDBY-TIMEOUT-DC 1440 &"C:\Program Files (x86)\Microsoft Office\OFFICE11\WINWORD.EXE" &POWERCFG /CHANGE /STANDBY-TIMEOUT-AC 30 &POWERCFG /CHANGE /STANDBY-TIMEOUT-DC 30
If you desire a batch file, this is how it can be done: the sleep timeout will be modified to last for 24 hours, and the program will be launched in a manner that waits until it is closed before restoring the sleep timeout to 30 minutes.
On a desktop:
@title Change power settings, open program, restore power settings @echo off setlocal enableextensions rem change timeout from 1 - 9999999 minutes POWERCFG /CHANGE /STANDBY-TIMEOUT-AC 1440 rem do not use: rem start "" "C:\Program Files (x86)\Microsoft Office\OFFICE11\WINWORD.EXE.lnk" "C:\Program Files (x86)\Microsoft Office\OFFICE11\WINWORD.EXE" POWERCFG /CHANGE /STANDBY-TIMEOUT-AC 30 exit /b
On a laptop both AC and DC:
@title Change power settings, open program, restore power settings @echo off setlocal enableextensions rem change timeout from 1 - 9999999 minutes POWERCFG /CHANGE /STANDBY-TIMEOUT-AC 1440 POWERCFG /CHANGE /STANDBY-TIMEOUT-DC 1440 rem do not use: rem start "" "C:\Program Files (x86)\Microsoft Office\OFFICE11\WINWORD.EXE.LNK" "C:\Program Files (x86)\Microsoft Office\OFFICE11\WINWORD.EXE" POWERCFG /CHANGE /STANDBY-TIMEOUT-AC 30 POWERCFG /CHANGE /STANDBY-TIMEOUT-DC 30 exit /b
Shortcut to Power Control Panel:
For more info from cmd:
powercfg /change /?
Learn how to personalize your power settings for any program you wish to execute with either a Windows shortcut or a cmd batch file. You can deactivate screen shutdown for particular software or turn it on for specific software.
Similarly, you can disable or enable sleep mode and disk sleep mode for particular software.