Page 1 of 1
Put the End Time the computer will sleep.
Posted: 09 Feb 2021 12:45
by PAB
Good evening,
The code below works . . .
Code: Select all
set /p "time=>Please enter the Time in minutes and press <Enter>: "
set /a time=%time%*60
if %time% GEQ 1 (
echo The computer is getting ready to go to Sleep . . .
timeout %time% /nobreak > nul
%SystemRoot%\system32\rundll32.exe powrprof.dll, SetSuspendState 0,1,0
echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit
) else (
echo ERROR: You MUST enter the number of MINUTES^!
echo ^>Press ANY key to return to the Options . . . & pause >nul & cls & goto :Options
)
Now I want to add the actual time [ Hours and Minutes only ] that it will actually go to sleep. The input is in
minutes. The below just puts the CURRENT time . . .
Code: Select all
set StartTime=%time%
set /p "time=>Please enter the Time in minutes and press <Enter>: "
set /a time=%time%*60
set /a time1=%time%*60
for /f "tokens=1*" %%a in ('PowerShell -NoP -C "(Get-Date).AddMinutes(%time1%).ToString('HH:mm')"') do (set "MyTime=%%a")
if %time% GEQ 1 (
echo The computer will go to sleep at %MyTime% . . .
timeout %time% /nobreak > nul
%SystemRoot%\system32\rundll32.exe powrprof.dll, SetSuspendState 0,1,0
echo ^>Press ANY key to EXIT . . . & pause >nul & Exit
) else (
echo ERROR: You MUST enter the number of MINUTES^!
echo ^>Press ANY key to return to the Options . . . & pause >nul & cls & goto :Options
)
I have tried using wmic, something like this . . .
for /f %%i in ('wmic OS Get LocalDateTime /Value ^& wmic Path Win32_LocalTime Get DayOfWeek /Value') do for /f %%j in ("%%i") do (set "%%j")
set "Hours=%LocalDateTime:~8,2%" & set "Minutes=%LocalDateTime:~10,2%"
. . . among other things but I can't seem to get it to work!
Any help will be greatly appreciated.
Thanks in advance.
Re: Put the End Time the computer will sleep.
Posted: 10 Feb 2021 06:10
by PAB
Good afternoon,
I also tried this which works as long as the the MINUTES are not equal to or greater than 120 . . .
Code: Select all
@echo off
set /A TimeTest=90
set hh=%time:~0,2%
if "%hh:~,1%"=="0" set hh=%hh:~1,2%
set mm=%time:~3,2%
if "%mm:~,1%"=="0" set mm=%mm:~1,2%
set /A mm=%mm%+%TimeTest%
::if %mm% GEQ 60 set /A mm=%mm%-60 && set /A hh=%hh%+1
::if %mm% GEQ 120 set /A mm=%mm%-120 && set /A hh=%hh%+2
if %mm% GEQ 60 set /A mm=%mm%-60 && set /A hh=%hh%+1
if %mm% GEQ 60 if %mm% GEQ 120 set /A mm=%mm%-120 && set /A hh=%hh%+2
if %hh% GEQ 24 set hh=00
if %mm% LSS 10 set mm=0%mm%
if %hh% LSS 10 set hh=0%hh:~1,1%
set hhmm=%hh%:%mm%
echo %hhmm%
pause & exit
Thanks in advance.
Re: Put the End Time the computer will sleep.
Posted: 10 Feb 2021 14:11
by T3RRY
With the aid of powershell:
- GetEnd.ps1
Code: Select all
Param($p1)
$offset = New-TimeSpan -minutes $P1
(Get-Date) + $Offset
Example Batch:
Code: Select all
@Echo off
:Input
Set /P "Minutes=Enter Minutes: "
Echo("%Minutes%"|%__APPDIR__%findstr.exe /RX "[0123456789""]*" > nul || Goto :Input
For /F "Delims=" %%G in ('powershell -executionpolicy bypass -file GetEnd.ps1 %Minutes%')Do Set "Sleep.time=%%G"
Echo(%Sleep.time%
Re: Put the End Time the computer will sleep.
Posted: 11 Feb 2021 03:22
by PAB
Thanks for the reply T3RRY, it is very much appreciated.
That works. It is obviously one of those situations where PS is far better at achieving something rather than using a Batch script.
Stay well and be SAFE.
Re: Put the End Time the computer will sleep.
Posted: 11 Feb 2021 12:09
by T3RRY
PAB wrote: ↑11 Feb 2021 03:22
Thanks for the reply
T3RRY, it is very much appreciated.
That works. It is obviously one of those situations where PS is far better at achieving something rather than using a Batch script.
Stay well and be SAFE.
Indeed. I really should learn more about powershell scripting myself.
If your interested, this is the resource I used for this:
https://docs.microsoft.com/en-us/powers ... rshell-7.1
For example I should have learned prior how to concatenate powershell commands to avoid the need for an external script like So:
Code: Select all
@Echo off
:Offset <Days| Minutes | Hours | Seconds>
Echo(%*|%__APPDIR__%findstr.exe /BEIC:"Days" /BEIC:"Minutes" /BEIC:"Hours" /BEIC:"Seconds" > nul || (
Echo( * Invalid Parameter supplied for %%1 *
%__APPDIR__%findstr.exe /BC:":Offset" "%~f0"
Goto :Eof
)
Set ".Offset="
Set /P ".Offset=Enter %~1: "
2> nul Set ".Offset" |%__APPDIR__%findstr.exe /RX "[\.][O][f][f][s][e][t][=][0123456789]*" > nul || Goto :Offset
For /F "UsebackQ Delims=" %%G in (`powershell.exe -command "$offset = New-TimeSpan -%~1 %.Offset%;(Get-Date) + $Offset"`)Do Set "Sleep.time=%%G"
Echo(%Sleep.time%
Exit /B 0
Re: Put the End Time the computer will sleep.
Posted: 11 Feb 2021 13:55
by PAB
Thanks again for the reply T3RRY, it is very much appreciated.
I couldn't get this to work in a Batch script because it didn't ask for the minutes to input.
Am I being a bit slow?
Re: Put the End Time the computer will sleep.
Posted: 11 Feb 2021 14:23
by T3RRY
Call it as you would Any function, supplying the offset type, in your usage case, Minutes:
How are you calling it? Can you show the code thats failing?
Re: Put the End Time the computer will sleep.
Posted: 11 Feb 2021 16:42
by PAB
I have to input the minutes twice, and then after it has finished, a message briefly pops up. I had to add a pause to see the output. Here is the code . . .
Code: Select all
@Echo off
Set /P "Minutes=Enter Minutes: "
Call :Offset Minutes
pause
:Offset <Days| Minutes | Hours | Seconds>
Echo(%*|%__APPDIR__%findstr.exe /BEIC:"Days" /BEIC:"Minutes" /BEIC:"Hours" /BEIC:"Seconds" > nul || (
Echo( * Invalid Parameter supplied for %%1 *
%__APPDIR__%findstr.exe /BC:":Offset" "%~f0"
Goto :Eof
)
Set ".Offset="
Set /P ".Offset=Enter %~1: "
2> nul Set ".Offset" |%__APPDIR__%findstr.exe /RX "[\.][O][f][f][s][e][t][=][0123456789]*" > nul || Goto :Offset
For /F "UsebackQ Delims=" %%G in (`powershell.exe -command "$offset = New-TimeSpan -%~1 %.Offset%;(Get-Date) + $Offset"`)Do Set "Sleep.time=%%G"
Echo(%Sleep.time%
Exit /B 0
Thanks in advance.
Re: Put the End Time the computer will sleep.
Posted: 11 Feb 2021 17:35
by T3RRY
When testing a script, call it from the command line so the console remains open after it's execution.
A modified and remarked version for your understanding:
Code: Select all
@Echo off & Goto :MAIN
:Offset <Days| Hours | Minutes | Seconds> </N (Negative)>
rem /* Assign Params value for testing */
Set "Params=%*"
rem /* Test for No args */
If "%Params%" == "" Goto :ParamErr.Offset
rem /* Test for Usage Arg */
If Not "%Params:/?=%" == "%Params%" Goto :Usage.Offset
rem /* Validate Arg 1 Option ; Show Error and Usage if Not */
Echo/%~1|%__APPDIR__%findstr.exe /BEIC:"Days" /BEIC:"Minutes" /BEIC:"Hours" /BEIC:"Seconds" > nul || (
:ParamErr.Offset
Echo/ * VALID Parameter for %%1 required:
:Usage.Offset
%__APPDIR__%findstr.exe /BC:":Offset" "%~f0"
Goto :Eof
)
rem /* Test for /N negative offset switch */
If Not "%Params:/N=%" == "%Params%" ( Set "Offset.Type=-" )Else Set "Offset.type=+"
:Offset.Def
rem /* Reset .Offset var and take input */
Set ".Offset="
Set /P ".Offset=Enter %~1: "
rem /* Validate Input is an Integer */
2> nul Set ".Offset" |%__APPDIR__%findstr.exe /RX "[\.][O][f][f][s][e][t][=][0123456789]*" > nul || Goto :Offset.Def
Rem /* Enact Powershell Command to Get Offset Date Time */
For /F "UsebackQ Delims=" %%G in (`powershell.exe -command "$offset = New-TimeSpan -%~1 %.Offset%;(Get-Date) %Offset.Type% $Offset"`)Do Set "Offset.time=%%G"
Echo/%Offset.time%
Exit /B 0
===========================
:MAIN :#Your script body below
PAB wrote: ↑11 Feb 2021 16:42
I have to input the minutes twice, and then after it has finished, a message briefly pops up. I had to add a pause to see the output. Here is the code . . .
I have to ask, Why would you add an additional Set /P Minutes when the function already asks for input of the Value?
If you wish for some reason to retain the entered minutes value after the function is called, It is available in the variable ".Offset":
Code: Select all
:MAIN :#Your script body below
:# Your script
Call :Offset Minutes
Set "Minutes=%.Offset%"
:# Your script after call
FYI, I don't tend to script single purpose functions, as it limits how useful they are to others, and even to the initial user should they have similar, but slightly different needs in the future.
The reason for the .Offset var being Defined within the function is to ensure it is Suitable as Input to the Powershell command.
Validation is enacted on Arg 1 and the assigned input of Set /P using findstr to ensure the powershell command is valid, with regards to both the measure and quantity of time.
As it's scripted the function can be used by you or others for determining an offset date / time in either + or - Days, Hours, Minutes or Seconds without requiring further modification, instead of being constrained to just getting a + minutes offset
Re: Put the End Time the computer will sleep.
Posted: 12 Feb 2021 04:26
by PAB
Thanks again for the reply T3RRY and detailed explanations, it is very much appreciated.
I will have to leave this. I am a 60+ somebody who just likes doing things to keep my brain active. I am not a programmer in any way, but unfortunately this is beyond my capabilities.
I did try and I just couldn't get it to work. I keep wanting to add another Set /P "Minutes=Enter Minutes: " and then Call :Offset Minutes, but I know that is wrong.
Thanks again, and I hope you and your family keep well and SAFE.
Re: Put the End Time the computer will sleep.
Posted: 12 Feb 2021 06:06
by T3RRY
Below is a script that the function used to effect your end goal, albeit in a slightly different way:
Code: Select all
@Echo off & Goto :MAIN
:Offset <Days| Hours | Minutes | Seconds> </N (Negative)>
rem /* Assign Params value for testing */
Set "Params=%*"
rem /* Test for No args */
If "%Params%" == "" Goto :ParamErr.Offset
rem /* Test for Usage Arg */
If Not "%Params:/?=%" == "%Params%" Goto :Usage.Offset
rem /* Validate Arg 1 Option ; Show Error and Usage if Not */
Echo/%~1|%__APPDIR__%findstr.exe /BEIC:"Days" /BEIC:"Minutes" /BEIC:"Hours" /BEIC:"Seconds" > nul || (
:ParamErr.Offset
Echo/ * VALID Parameter for %%1 required:
:Usage.Offset
%__APPDIR__%findstr.exe /BC:":Offset" "%~f0"
Goto :Eof
)
rem /* Test for /N negative offset switch */
If Not "%Params:/N=%" == "%Params%" ( Set "Offset.Type=-" )Else Set "Offset.Type=+"
:Offset.Def
rem /* Reset .Offset var and take input */
Set ".Offset="
Set /P ".Offset=Enter %~1: "
rem /* Validate Input is an Integer */
2> nul Set ".Offset" |%__APPDIR__%findstr.exe /RX "[\.][O][f][f][s][e][t][=][0123456789]*" > nul || Goto :Offset.Def
Rem /* Enact Powershell Command to Get Offset Date Time */
For /F "UsebackQ Delims=" %%G in (`powershell.exe -command "$offset = New-TimeSpan -%~1 %.Offset%;(Get-Date) %Offset.Type% $Offset"`)Do Set "Offset.time=%%G"
Exit /B 0
===========================
:MAIN
Call :Offset Minutes
For /F %%a in ('Echo prompt $E^|cmd')Do set "\E=%%a"
Setlocal enabledelayedexpansion
Set "end="&CLS
For /L %%. in ()Do If "!end!" == "" (
For /F "Delims=" %%v in ('Powershell -Command Get-Date')Do If not "!Offset.Time:%%v=!" == "!Offset.Time!" (
Set "End=1"
powershell.exe -command "Add-Type -AssemblyName System.Windows.Forms;$PowerState = [System.Windows.Forms.PowerState]::Suspend;$Force = $false;$DisableWake = $false;[System.Windows.Forms.Application]::SetSuspendState($PowerState, $Force, $DisableWake);"
)Else ( <Nul Set /P "=%\E%[1;1H%\E%[32m[%%v]%\E%[2;1H%\E%[31m[!Offset.Time!]%\E%[0m" )
)Else Exit
I will have to leave this. I am a 60+ somebody who just likes doing things to keep my brain active. I am not a programmer in any way, but unfortunately this is beyond my capabilities.
Never underestimate yourself. I began learning scripting 14 months ago, for much the same reason as yourself, and I'm no spring chicken either. I've not bothered taking any computer science or maths courses I'd need to in order to call myself a developer, but I approach the coding I do as an excercise in problem solving, and use s.m.a.r.t prolem solving steps to Get from scratch to script. There's an abundance of information available to be learnt at the pace that suits you, or even just to select information from that will assist in solving a given problem.
Re: Put the End Time the computer will sleep.
Posted: 12 Feb 2021 06:36
by miskox
PAB wrote: ↑09 Feb 2021 12:45
Good evening,
The code below works . . .
Code: Select all
set /p "time=>Please enter the Time in minutes and press <Enter>: "
set /a time=%time%*60
if %time% GEQ 1 (
echo The computer is getting ready to go to Sleep . . .
timeout %time% /nobreak > nul
%SystemRoot%\system32\rundll32.exe powrprof.dll, SetSuspendState 0,1,0
echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit
) else (
echo ERROR: You MUST enter the number of MINUTES^!
echo ^>Press ANY key to return to the Options . . . & pause >nul & cls & goto :Options
)
Now I want to add the actual time [ Hours and Minutes only ] that it will actually go to sleep. The input is in
minutes. The below just puts the CURRENT time . . .
Code: Select all
set StartTime=%time%
set /p "time=>Please enter the Time in minutes and press <Enter>: "
set /a time=%time%*60
set /a time1=%time%*60
for /f "tokens=1*" %%a in ('PowerShell -NoP -C "(Get-Date).AddMinutes(%time1%).ToString('HH:mm')"') do (set "MyTime=%%a")
if %time% GEQ 1 (
echo The computer will go to sleep at %MyTime% . . .
timeout %time% /nobreak > nul
%SystemRoot%\system32\rundll32.exe powrprof.dll, SetSuspendState 0,1,0
echo ^>Press ANY key to EXIT . . . & pause >nul & Exit
) else (
echo ERROR: You MUST enter the number of MINUTES^!
echo ^>Press ANY key to return to the Options . . . & pause >nul & cls & goto :Options
)
I have tried using wmic, something like this . . .
for /f %%i in ('wmic OS Get LocalDateTime /Value ^& wmic Path Win32_LocalTime Get DayOfWeek /Value') do for /f %%j in ("%%i") do (set "%%j")
set "Hours=%LocalDateTime:~8,2%" & set "Minutes=%LocalDateTime:~10,2%"
. . . among other things but I can't seem to get it to work!
Any help will be greatly appreciated.
Thanks in advance.
Isn't the reason that PAB uses time as a variable name?
Code: Select all
set /p "time=>Please enter the Time in minutes and press <Enter>: "
set /a time=%time%*60
Saso
Re: Put the End Time the computer will sleep.
Posted: 13 Feb 2021 03:41
by PAB
Thanks again for the reply T3RRY, it is very much appreciated.
I still can't get my head round how to get this to work. If I run the code it asks for the minutes, I enter those and press enter and I don't get the revised time.
Thank you for the time and effort you have put into this. It is not important so please don't spend any more time on it.
Stay SAFE, Best Regards, Paul.
Re: Put the End Time the computer will sleep.
Posted: 16 Feb 2021 10:22
by Eureka!
Maybe this can help you:
Code: Select all
@echo off
setlocal
set MINUTES=12
for /f "usebackq delims=" %%t in (`powershell.exe -ExecutionPolicy Bypass -noprofile -nologo -command ^(get-date^).AddMinutes^(%MINUTES%^)`) DO set BEDTIME=%%t
echo BEDTIME = %BEDTIME%
pause
The tricky part is escaping special characters between (` and `)