用于检查系统是否在特定时间之间的 VB 脚本 [英] VB Script to check if system is between a certain time

查看:15
本文介绍了用于检查系统是否在特定时间之间的 VB 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑以下脚本以检查系统时间是否在凌晨 3:05 和早上 5 点 15 分.如果系统在这些时间之间,脚本应该显示 timechecksuccess.flag,如果系统在这些时间之外,则应该显示 timecheckfail.flag.我的脚本并没有像它应该的那样失败.你能告诉我我哪里出错了.谢谢

Editing the following Script that will check if system time is between 3:05am & 5:15am. Script should show a timechecksuccess.flag if system is between those hours and timecheckfail.flag if system is outside of those times. My script is not failing as it should. Can you tell me where I'm going wrong. Thanks

Option Explicit`enter
On Error Resume Next

Dim g_objShell, g_strTSuccess, g_strTFail, g_objFS, g_strDone

Set g_objShell = CreateObject("WScript.Shell")
Set g_objFS = CreateObject("Scripting.FileSystemObject")

g_strTSuccess = "C:\TimeCheckSuccess.Flag"
g_strTFail = "C:\TimeCheckFail.Flag"
g_strDone = "C:\Done.Flag"

If g_objFS.FileExists(g_strTSuccess) Then
Call g_objFS.DeleteFile(g_strTSuccess, True)
End If

If g_objFS.FileExists(g_strTFail) Then
Call g_objFS.DeleteFile(g_strTFail, True)
End If

If g_objFS.FileExists(g_strDone) Then
Call g_objFS.DeleteFile(g_strDone, True)
End If

If DatePart("h" ("n", Now()) < 0305 or DatePart("h" ("n", Now()) > 0515 or                    WScript.Arguments.Named.Exists("Now") ))Then

Call g_objFS.CreateTextFile(g_strTSuccess, True)    Else
Call g_objFS.CreateTextFile(g_strTFail, True)   

如果结束

 Call g_objFS.CreateTextFile(g_strDone, True)

Set g_objShell = Nothing
Set g_objFS = Nothing

Wscript.Quit

推荐答案

If Time() > TimeValue("3:45am") then msgbox "After 3:45am"
If Time() < TimeValue("8:00pm") then msgbox "Before 8pm"

而不是这种编码.

If g_objFS.FileExists(g_strDone) Then
    Call g_objFS.DeleteFile(g_strDone, True)
End If

删除文件即可.你不测试然后做,你做然后测试.你也不用调用.如果您关心它是否成功删除.

Just delete the file. You don't test then do, you do then test. You also don't use call. If you care if it sucessfully deleted or not.

on error resume next
g_objFS.DeleteFile("C:\Done.Flag", True)
If err.number = 53 Then 
    msgbox "File didn't exist"
elseif err.number <> 0 then 
    msgbox "unknown error"
End If
on error goto 0

同样,将 wscript.quit 作为文件的最后一行也是毫无意义的.

Also it is pointless to have a wscript.quit as the last line of the file.

这篇关于用于检查系统是否在特定时间之间的 VB 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆