Windows 10 timer to turn off the PC

3

I want to make a small timer for my pc that does the following:

You must pick up and save the current time in a variable and if, for example

  • The time is between 00:00:00 and 02:00:00 that the equipment turns off in 2 hours

    shutdown -s -t 7200
    
  • And if it's more than 02:00:00, the shutdown is in 1 hour instead of two.

I know I have to use IF and AND in the first condition, and the clear shutdown is, but I do not know in .bat how to do the truth.

I've also read that maybe it's easier with PowerShell. I do not know the truth.

    
asked by Javilpc 01.11.2016 в 03:03
source

5 answers

1

The following may work. I have not tried it between midnight and two in the morning or between noon and two in the afternoon.

$ahora = get-date
if ($ahora.hour -lt 2) {$seg = 7200} else {$seg = 3600}
shutdown -s -t $seg

There are more elegant ways to achieve the same. This example only uses the most basic powershell. shutdown is not part of powershell, but is accessible within powershell.

    
answered by 28.11.2016 в 16:20
1
@echo off
TITLE Apagar
MODE con: cols=50 lines=40
COLOR 02
:menu
echo                 iP@@@@@@@2     iOMBMBMBB@  5BMBMBB@B    
echo               5@B@B@@@B@B@BU  i@B@B@B@B@B B@B@B@B@@@   
echo             :B@B@@@SUuBB@B@Bv MB@B@M@@@Bi @B@@@B@B@,    
echo            .B@B@M      .B@B@B B@B@       kB@Bv           
echo            @@B@Z        7@@B@ MB@@,::ir: B@B@L,:ii:      
echo           r@B@B         r@B@@ B@B@B@@@Bq @B@B@B@B@1         
echo           E@@B@         @B@@2 @B@B@B@B@ LB@B@B@B@B       
echo           L@B@B2      .@@@B@ O@@B       B@B@:             
echo            B@B@B@7,,vM@B@B@ vB@BS      .@B@@           
echo             B@B@B@B@B@B@BO  @@B@,      EB@BM            
echo              r@B@B@B@B@F.  r@B@B       B@B@r             
echo                 ,;;:.      ..          ,  . 
echo.            tiempo de 2.horas = 7200
echo.              DIA %date%  HORA %time%
set /p tiempo=El tiempo de apagar :
if %tiempo% =="7200" goto end
if %time% ==""02:00:00,00"" goto end   
goto menu

:end
shutdown -s -t %tiempo% -c "La computadora se apagará automáticamente
"La computadora se apagará automáticamente
goto exit

Copy to a notebook and change the extension for a .bat I HOPE IT WORKS

    
answered by 05.12.2018 в 14:51
0

Try this:

@echo off

set "now=%time: =0%"

if "%now%" GEQ "00:00:00,00" (

      if "%now%" LEQ "02:00:00,00" (

           shutdown.exe -s -t 7200

      )
)

if "%now%" GTR "02:00:00,00" (

      shutdown.exe -s -t 3600

)

I hope it works for you!

    
answered by 02.11.2016 в 14:34
0

I really liked your question, I thought it was very interesting so I decided to investigate and try it, this was the code I made from what I found:

@echo off

SET HOUR=%time:~0,2%
IF "%hour:~0,1%" == " " SET HOUR=0%HOUR:~1,1%

SET MIN=%time:~3,2%
IF "%min:~0,1%" == " " SET MIN=0%MIN:~1,1%

SET SECS=%time:~6,2%
IF "%secs:~0,1%" == " " SET SECS=0%SECS:~1,1%

SET TIME_TO_SHOW=%HOUR%:%MIN%:%SECS%
SET TIME_TO_VALIDATE=%HOUR%%MIN%%SECS%

SET SECONDS_TO_SHUTDOWN=999999999999

IF %TIME_TO_VALIDATE% GEQ 000000 IF %TIME_TO_VALIDATE% LEQ 020000 SET SECONDS_TO_SHUTDOWN=7200

IF %TIME_TO_VALIDATE% GTR 020000 SET SECONDS_TO_SHUTDOWN=3600

ECHO La hora actual es: %TIME_TO_SHOW%, el computador se apagara en -T %SECONDS_TO_SHUTDOWN%

SHUTDOWN -s -t %SECONDS_TO_SHUTDOWN%

PAUSE > NUL

It is a simple flat text file saved with the extension .bat , I tried it and it worked for me correctly validating the time and "setting" correctly the "argument" -t , in scripts batchs Conditional operators are the following (so you understand how the previous code works):

EQU - equal - same as

NEQ - not equal - not equal - different from

LSS - less than - less than

LEQ - less than or equal - less than or equal to

GTR - greater than - more than

GEQ - greater than or equal - more or equal than

We must also say that the operators AND and OR do not exist but you can validate these with a double if (like the one that appears in the code)

    
answered by 02.11.2016 в 15:08
0

Very good question, you could use the following code in Powershell, you can run it directly in the console or save it as a file with extension "ps1". The first line keeps the current date and time variable and then makes comparisons. From the second to the fifth line, make an IF-ELSE where you compare the current schedule with the rank you requested. (00AM to 2AM) "gt" and "lt" are the comparison operators meaning "greater than" and "lower than" respectively. I hope it serves you.

$actual = (Get-Date) 
if ($actual -gt "00:00:00" -and $actual -lt "02:00:00") 
    {$tiempo =  "7200" } 
else 
    {$tiempo = "3600"}
shutdown -s -t $tiempo
    
answered by 04.02.2017 в 15:01