Solved

TIMECHECK.VBS

Posted on 2013-11-04
4
144 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …

895 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

18 Experts available now in Live!

Get 1:1 Help Now