Sign in to follow this  
Followers 0
gil900

ShutdownMyComputer - can't shutdown your computer and go to sleep? no it will be more easy

1 post in this topic

#1 ·  Posted (edited)

Hello!

Do you have the problem that you can't go to sleep at night because you can not close the computer?

With my script you can defive the hours and days when the script will hibernate the computer.

and if the script was hibernated the computer and you will reopen the computer again while the computer is supposed to be closed/hibernated, then the script will immediately hibernate again the computer!

so reopen the computer will not help you! the computer will immediately hibernate again.

you will able to reopen the computer when the the time is allowed time.

 

Script:

#NoTrayIcon
#include <Array.au3>
#include <File.au3>


Global $ScriptName = "shutdown my computer"
Global $SettingsFile = @ScriptDir&"\Settings.txt"
Global $ShutDownTimes , $AntiClose , $ShutdownType , $TodayShutDowns

If Not FileExists($SettingsFile) Then WriteNewSettingsFile()

UpdateSettings()



$Today = -1
$SleepTime = 2000
$ShutdownTimer = 0
;$timer = TimerInit()
While 1
    If $Today <> @WDAY Then
        $TodayShutDowns = GetTodayShutDowns($ShutDownTimes)
        ;_ArrayDisplay($TodayShutDowns)
        $Today = @WDAY
    EndIf
    If $TodayShutDowns[0] > 0 Then
        $a = 1
        Do
            $TimeInRage = TimeInRage($TodayShutDowns[$a])
            If $TimeInRage <> 0 Then
                If $TimeInRage = 1 Then
                    $Shutdown = 1
                    $NOShutDown_ProcessList = FileArrayRead($SettingsFile,"NOShutDown_ProcessList")
                    If IsArray($NOShutDown_ProcessList) And $NOShutDown_ProcessList[0] > 0 Then
                        For $a2 = 1 To $NOShutDown_ProcessList[0]
                            If ProcessExists($NOShutDown_ProcessList[$a]) Then
                                $Shutdown = 0
                                ExitLoop
                            EndIf
                        Next
                    EndIf
                    If $Shutdown = 1 Then
                        ;MsgBox(0,"","Shut down the computer!!") ; Shut down the computer!!
                        If $ShutdownTimer = 0 Then
                            $ShutdownTimer = 1
                            $timer2 = TimerInit()
                            If Shutdown($ShutdownType) = 0 Then
                                MsgBox(16,"Error","Shutdown Error")
                                Exit
                            EndIf
                        Else
                            If TimerDiff($timer2) > 20000 Then $ShutdownTimer = 0
                        EndIf
                    EndIf

                EndIf
                $a = $a+1
            Else
                _ArrayDelete($TodayShutDowns,$a)
                $TodayShutDowns[0] = $TodayShutDowns[0]-1
            EndIf
        Until $a > $TodayShutDowns[0]
    EndIf

    ;If TimerDiff($timer) > 60000 Then
    ;   UpdateSettings()
    ;   $timer = TimerInit()
    ;EndIf
    Sleep($SleepTime)
WEnd



Func GetTodayShutDowns($ShutDownTimes)
    Local $TodayShutDowns[1]
    $a1ShutDownTimes = StringSplit($ShutDownTimes,"=",1)
    $aAddShutDown = 1
    If $a1ShutDownTimes[0] = 2 Then
        $a2ShutDownTimes = StringSplit($a1ShutDownTimes[1],",",1)
        If _ArraySearch($a2ShutDownTimes,"Day"&Number(@WDAY),1) <= 0 Then $aAddShutDown = 0
        If $aAddShutDown = 1 Then $HShutDownTimes = $a1ShutDownTimes[2]
    ElseIf $a1ShutDownTimes[0] = 1 Then
        $HShutDownTimes = $a1ShutDownTimes[1]
    EndIf

    If $aAddShutDown = 1 Then
        $aHShutDownTimes = StringSplit($HShutDownTimes,",",1)
        For $a = 1 To $aHShutDownTimes[0]
            If TimeInRage($aHShutDownTimes[$a]) >= 1 Then _ArrayAdd($TodayShutDowns,$aHShutDownTimes[$a])
        Next
    EndIf
    $TodayShutDowns[0] = UBound($TodayShutDowns)-1
    Return $TodayShutDowns
EndFunc


Func TimeInRage($TimeString) ; Get input like 22:00 or 22:00-23:00 and return if the time Happening/Will happen/Was happened/error input
    Local $Output = 0 , $sTimeString

    $sTimeString = StringSplit($TimeString,"-",1)
    If $sTimeString[0] <> 1 And $sTimeString[0] <> 2 Then Return 0
    For $a = 1 To $sTimeString[0]
        If TimeValid($sTimeString[$a]) = 0 Then Return 0
    Next

    If $sTimeString[0] = 1 Then
        $s2TimeString_1  = StringSplit($sTimeString[1],":",1)
        If $s2TimeString_1[1] >= @HOUR And $s2TimeString_1[2] > @MIN Then
            Return 2
        ElseIf $s2TimeString_1[1] = @HOUR And $s2TimeString_1[2] = @MIN Then
            Return 1
        Else
            Return 0
        EndIf
    Else
        $s2TimeString_1 = StringSplit($sTimeString[1],":",1)
        $s2TimeString_2 = StringSplit($sTimeString[2],":",1)
        If $s2TimeString_2[1] <= @HOUR And $s2TimeString_2[2] < @MIN Then Return 0
        If $s2TimeString_1[1] <= @HOUR And $s2TimeString_1[2] <= @MIN Then Return 1
        Return 2
    EndIf
EndFunc ; 0 = Was happened/error , 1 = Happening now , 2 = Will happen

Func TimeValid($input)
    Local $tmp , $Output = 0
    $tmp = StringSplit($input,":",1)
    If $tmp[0] = 2 Then
        If StringIsDigit($tmp[1]) And StringIsDigit($tmp[2]) Then
            If $tmp[1] >= 0 And $tmp[1] <= 23 And $tmp[2] >= 0 And $tmp[2] <= 59 Then $Output = 1
        EndIf
    EndIf
    Return $Output
EndFunc



Func UpdateSettings()
    Local $ShutDownTimes_New
    $ShutDownTimes_New = StringStripWS(IniRead($SettingsFile,"Settings","ShutDownTimes","0"),3)
    If $ShutDownTimes_New <> $ShutDownTimes Then
        $ShutDownTimes = $ShutDownTimes_New
        $TodayShutDowns = GetTodayShutDowns($ShutDownTimes)
    EndIf
    If $ShutDownTimes = "" Then Exit
    $AntiClose = IniRead($SettingsFile,"Settings","AntiClose","0")
    If $AntiClose <> "0" And $AntiClose <> "1" Then $AntiClose = "0"
    $ShutdownType = IniRead($SettingsFile,"Settings","ShutdownType","")
EndFunc

Func FileArrayRead($File, $ArrayName)
    Local $OutPut = 0, $aTmp[1] , $aFile , $FondArray , $a
    ;$aFile = $GetStartEndAerea[0]
    $aFile = StringSplit(FileRead($File),@CRLF,1)

    $FondArray = _ArraySearch($aFile, "[[" & $ArrayName & "]]", 1)
    If $FondArray > 0 Then
        If $FondArray + 1 < $aFile[0] Then
            For $a = $FondArray + 1 To $aFile[0]
                If $aFile[$a] = "[[END]]" Then ExitLoop
                _ArrayAdd($aTmp, $aFile[$a])
            Next
            $aTmp[0] = UBound($aTmp) - 1
            $OutPut = $aTmp
        EndIf
    EndIf

    Return $OutPut
EndFunc   ;==>FileArrayRead


Func WriteNewSettingsFile($ExitOnError = 1)
    Local $Output , $hFile
    $hFile = FileOpen($SettingsFile, 2 + 8 + 32)
    If $hFile <> -1 Then
        $Output = FileWrite($SettingsFile, "[Settings]"&@CRLF&"AntiClose=0"&@CRLF&"ShutDownTimes=0"&@CRLF&"ShutdownType=64"&@CRLF&@CRLF&"[[NOShutDown_ProcessList]]"&@CRLF&"[[END]]")
    EndIf
    If $ExitOnError = 1 Then
        If $Output = 0 Or $hFile = -1 Then
            MsgBox(16, "Error", "Could not create the settings file. As a result, "&$ScriptName&" will exit."&@CRLF&@CRLF& _
                "The folder might have no writing permissions or the software is running in sandbox by your AntiVirus."&@CRLF&@CRLF& _
                "Please check if the software running in sandbox by your AntiVirus. if this is the problem, add the software to trusted files "& _
                "in your AntiVirus."&@CRLF&"If it did not help, try to move the software to another folder with writing permissions" )
            Exit
        EndIf
    EndIf
    FileClose($hFile)
    Return $Output
EndFunc



Func ExitTest()
    Exit
EndFunc

In addition, you can set the type of the shutdown to something else then Hibernate.

to do it you need to chage the Value of "ShutdownType" in the settings file to another value.

the value should be a number representing the type of the Shutdown.

I recommend leaving it 64 because in this way when the computer turns off, your work in the computer will not go..

 

Enjoy!

EDIT:

how to define the shutdown times:

for example if you want that that the shutdown times will be every day and and when the time is between 23:00 to 23:59 and between 00:00 to 07:00 then you need to write in the settings file in "ShutDownTimes":

 

23:00-23:59,00:00-07:00

 

If you want that it will only happen on certain days, for example in days 1 , 2 , 3 then you need to write:

Day1,Day2,Day3=23:00-23:59,00:00-07:00
Edited by gil900

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0