I wanted to prepare a .BAT
file to automatically shut down the computer after a desired time.
And according to the information I got, I was able to create a code directory like the one below.
But at some point it doesn't accomplish anything I want.
@echo off
setlocal
set /p hours=Please enter the number of hours to shut down your computer (leave blank for only minutes):
set /p minutes=Please enter the number of minutes to shut down your computer:
set total_minutes=0
if defined hours set /a total_minutes=%hours% * 60
set /a total_minutes+=minutes
REM Check if there is an active shutdown request
for /F "tokens=2,3,4 delims=:." %%a in ('shutdown /a 2^>nul ^| findstr /C:"time remaining"') do (
set /a remaining_minutes=%%a*60+%%b
set /a elapsed_minutes=%total_minutes% - %remaining_minutes%
set /a new_total_minutes=%total_minutes% - %elapsed_minutes%
)
REM Display active shutdown request countdown if applicable
if defined remaining_minutes (
echo There is a shutdown request currently active.
echo The duration of the shutdown request is %remaining_minutes% minutes, and %elapsed_minutes% minutes have passed since the shutdown request was created.
echo %remaining_minutes% minutes remaining until shutdown.
echo Press Ctrl+C to cancel the shutdown.
echo.
choice /T 60 /C CN /D C > nul
if errorlevel 2 (
echo Continuing with the shutdown.
goto SHUTDOWN
) else (
echo Shutdown canceled.
exit /b
)
)
REM Display initial countdown information if no active shutdown request
echo Your computer will be shut down in %total_minutes% minutes.
echo Press Ctrl+C to cancel the shutdown.
echo.
choice /T 60 /C CN /D C > nul
if errorlevel 2 (
echo Continuing with the shutdown.
goto SHUTDOWN
) else (
echo Shutdown canceled.
exit /b
)
:SHUTDOWN
shutdown /s /t 0
1) When we run the bat file as "Administrator"
, we first encounter a screen asking how many hours we want the computer to be shut down.
When we leave this part blank, we can log in directly in minutes. Or by answering "1"
to the first question and "45"
to the second question, we can set the computer to shut down after "1 Hour and 45 Minutes"
.
However, when I want to add a countdown that shows the current screen to be turned off in 60 seconds
after entering this information, I cannot do it.
It needs to give us a 60 second countdown to cancel the current transaction; but I can't see any countdown on the screen. And the screen turns off automatically after 60 seconds.
2) When I run the .BAT
File again, I want it to check if there is an active shutdown request and present it to us.
However, when I run the file, it does not present this information to us.
I tried to do this; but it didn't work. If there is an existing shutdown request, it should specify each time the .BAT file is run.
Example: There is an active shutdown request currently set to 95 minutes
. This request was created 15 minutes
ago and has 80 minutes
until the computer shuts down.
If you want to cancel this request, please press CTRL+C
.
I don't know if this is possible; However, I think that the .BAT file can create a .TXT
file in the %TEMP%
folder and present us whether there is an active shutdown and the remaining time according to the information in that .TXT file.
Or another way.
If there is someone who has knowledge on this subject, I would like him to know that I am expressing my gratitude with all my sincerity.