Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping VBScript

I'm trying to run a VBScript checking the time until it reaches 22:00 (10PM), and then runs shutdown.bat. I always get errors such as "'loop' without 'do'". Can anyone look at my code and see if there's a way to fix it?

Do 
    If Hour(Time()) => 22 And Minute(Time()) => 30 And Hour(Time()) < 23 Then
Loop Until True
    Then
        'Dim shell
        Set shell = CreateObject("WScript.Shell")
        shell.Run "shutdown.bat"
        Set shell = Nothing
    End If
like image 687
H3153N Avatar asked May 07 '26 14:05

H3153N


2 Answers

If you really want to keep a loop running until it reaches a specific hour, here's how it's done:

Do While True ' = Do the following forever (or until something breaks the loop)
    If Hour(Time()) => 22 And Minute(Time()) => 30 And Hour(Time()) < 23 Then
        ' Do whatever you want and then
        ' break the loop:
        Exit Do
    End If
Loop

However, I wouldn't recommend that because it will consume much resources. Instead, you should be using Task Scheduler.

Hope that helps :)

like image 188
41686d6564 stands w. Palestine Avatar answered May 09 '26 22:05

41686d6564 stands w. Palestine


Loop until the current time is greater than your desired end time:

endtime = CDate("22:30")
Do Until Time > endtime
  WScript.Sleep 100
Loop
CreateObject("WScript.Shell").Run "shutdown.bat"

Using WScript.Sleep inside the loop ensures that the loop doesn't do a burn-in on the CPU.

However, as @AhmedAbdelhameed already mentioned, creating a scheduled task that runs shutdown.bat at the desired time would usually be far more efficient.

like image 33
Ansgar Wiechers Avatar answered May 09 '26 22:05

Ansgar Wiechers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!