简体   繁体   English

循环 VBScript

[英]Looping VBScript

I'm trying to run a VBScript checking the time until it reaches 22:00 (10PM), and then runs shutdown.bat .我正在尝试运行 VBScript 检查时间,直到它到达 22:00(晚上 10 点),然后运行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

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 .相反,您应该使用Task Scheduler

Hope that helps :)希望有帮助:)

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.在循环内使用WScript.Sleep可确保循环不会对 CPU 进行老化。

However, as @AhmedAbdelhameed already mentioned, creating a scheduled task that runs shutdown.bat at the desired time would usually be far more efficient.但是,正如@AhmedAbdelhameed已经提到的,创建一个在所需时间运行shutdown.bat的计划任务通常会更有效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM