Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TIMECHECK.VBS

Posted on 2013-11-04
4
Medium Priority
?
159 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Starting up a Project

916 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