Solved

TIMECHECK.VBS

Posted on 2013-11-04
4
141 Views
Last Modified: 2013-11-07
Good morning,

I'm working on the following and was wondering if you can offer some suggestions.  At the time of installation I need this script to check to see if the system (Windows Server 2008)  is between the times of 3:05am & 5:15am.   If this is true create a 'timechecksuccess.flag' file.  If script is run outside of these times create a 'timecheckfail.flag'.  I'm testing script outside of those times and it's not failing.  Can you tell me where I'm going wrong.  


Option Explicit
'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", Now()) < 03 or DatePart("n", Now()) > 05 or DatePart("h", Now()) < 05 or DatePart("n", Now()) > 15 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
0
Comment
Question by:SDJ_1
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
This can be done in a number of ways but to stay close to your intended code I've made a function that checks if a given time is between 2 limits by looking at hour and minute separately. It could also be done by making a string containing "hh:mm" and comparing that to start/end time in strings, making sure they are formatted the same.

Note that in the code below, strictly speaking the check for end time may need to exclude the 'equals' but it may not be that important for you. The reason I want to mention it is that when you say 'between 3:05 and 5:15' you may want to exclude 5:15:01 or even 5:15:00.

The important change in the code is the seperate hour check: to check if a time is after 3:05 you check that:
- either the hour > 3, or
- the hour = 3 and the minute >= 05

And similar for end time, here is the code:
Option Explicit
'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:\temp\TimeCheckSuccess.Flag"
g_strTFail = "C:\temp\TimeCheckFail.Flag"
g_strDone = "C:\temp\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 TimeCheck(Now(), 03, 05, 05, 15) 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

Function TimeCheck(p_datDate, p_intHour1, p_intMinute1, p_intHour2, p_intMinute2)
   Dim l_intHour, l_intMinute
   l_intHour = DatePart("h", p_datDate)
   l_intMinute = DatePart("n", p_datDate)
   TimeCheck = (l_intHour > p_intHour1 Or (l_intHour = p_intHour1 And l_intMinute >= p_intMinute1)) And _
               (l_intHour < p_intHour2 Or (l_intHour = p_intHour2 And l_intMinute <= p_intMinute2))
End Function

Open in new window

0
 

Author Comment

by:SDJ_1
Comment Utility
Thanks for the help.   Script is running but it did not produce the .txt files
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
Oh sorry, probably because I changed the folder to C:\temp\

Here is a copy of my earlier post but reverted the paths to C:\
Option Explicit
'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 TimeCheck(Now(), 03, 05, 05, 15) 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

Function TimeCheck(p_datDate, p_intHour1, p_intMinute1, p_intHour2, p_intMinute2)
   Dim l_intHour, l_intMinute
   l_intHour = DatePart("h", p_datDate)
   l_intMinute = DatePart("n", p_datDate)
   TimeCheck = (l_intHour > p_intHour1 Or (l_intHour = p_intHour1 And l_intMinute >= p_intMinute1)) And _
               (l_intHour < p_intHour2 Or (l_intHour = p_intHour2 And l_intMinute <= p_intMinute2))
End Function

Open in new window

0
 

Author Comment

by:SDJ_1
Comment Utility
That did it.  Running some more test.  Thanks for your help!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now