Solved

Date string comparison in VBS

Posted on 2008-10-15
5
493 Views
Last Modified: 2010-04-24
I am trying to read printer event log record and just sect certain dates. The relevant lines of the code are as below.

Why does it still return dates where

intTempDate = 20081014
intTempDate = 20081013

and smaller?
intDate = 20081014
 
Function GetLog()
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colLoggedEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent Where Logfile = 'System'")
for each LogEvent in colLoggedEvents
     	intTempDate = Left(LogEvent.TimeWritten,8)
	If intTempDate > intDate Then
	     'WSHShell.Popup, 100, "Script Processing Message", 64
	     If LogEvent.EventCode = "1111" then
             'WSHShell.Popup intTempDate - intDate & " is less than " & intNumDays & vbCrLf & intTempDate & " is greater than " & (intDate - 1) , 10, "Script Processing Message", 64
	             ' Do Stuff
	     End if
     End If
Next
set smessage = Nothing
End Function

Open in new window

0
Comment
Question by:cescentman
[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
5 Comments
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 50 total points
ID: 22723599
Hello cescentman,

You may have a data type problem, as intDate is a Long while intTempDate is actually a String.

Use an explicit type conversion to force them both into the same type:

      If CLng(intTempDate) > CLng(intDate) Then

or the like...

Regards,

Patrick
0
 
LVL 18

Expert Comment

by:exx1976
ID: 22724247
I agree that it's most likely a data type problem, but I wouldn't use Longs for the comparison..  You are working with dates, so why not compare them as dates?

Use cDate to caste them both to dates, then do the comparison..  You'll need to get the format right, but it shouldn't be too painful.  Let me know if you need help with that.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 200 total points
ID: 22725774
Hi, you can actually restrict the date range during your Win32_NTLogEvent query, by using the TimeWritten property.

Regards,

Rob.
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
objSWbemDateTime.SetVarDate #10/14/2008#, True
 
Function GetLog()
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colLoggedEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent Where Logfile = 'System'" & _
	"AND TimeWritten >= '" & objSWbemDateTime.Value & "'")
For Each LogEvent in colLoggedEvents
	     'WSHShell.Popup, 100, "Script Processing Message", 64
	     If LogEvent.EventCode = "1111" then
             'WSHShell.Popup intTempDate - intDate & " is less than " & intNumDays & vbCrLf & intTempDate & " is greater than " & (intDate - 1) , 10, "Script Processing Message", 64
	             ' Do Stuff
	     End if
Next
set smessage = Nothing
End Function

Open in new window

0
 
LVL 1

Author Comment

by:cescentman
ID: 22728233
Many thanks for all 3 answers.

Patrick that works a treat.

exx1976 I didn't check this as I'm not sure how this would be an advantage ofer the previuos suggestion as it would achieve the same thins but add extra code.

Rob Works a treat and cuts the processing time to a third of the previous code - brilliant
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22728249
No problem at all.  Thanks for the grade.  Anything you can do to immediately restrict a WMI query will reduce the processing time, as opposed to returning all records, then sifting through them...

Regards,

Rob.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WMIC Output Formatting 1 84
Powershell PGP and SFTP 1 47
Cannot exit IF statement in PowerShell 4 47
Copy Sheet to New Workbook based on Cell Value 6 58
This article will show, step by step, how to integrate R code into a R Sweave document
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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