Solved

TIMECHECK.VBS

Posted on 2013-11-04
4
149 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
ID: 39623649
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
ID: 39630032
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
ID: 39630046
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
ID: 39630070
That did it.  Running some more test.  Thanks for your help!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

821 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