VB Script to check if system is between a certain time

喜夏-厌秋 提交于 2019-12-13 02:48:45

问题


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)   

End If

 Call g_objFS.CreateTextFile(g_strDone, True)

Set g_objShell = Nothing
Set g_objFS = Nothing

Wscript.Quit

回答1:


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

and instead of this coding.

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

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



来源:https://stackoverflow.com/questions/19762371/vb-script-to-check-if-system-is-between-a-certain-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!