Solved

TIMECHECK.VBS

Posted on 2013-11-04
4
151 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Simple Linear Regression

729 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