Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript Method to check date in Text File 1

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
Hi, can someone point out to me where I am going wrong in this code? I am simply trying to check a date in a text file and compare it to today. If the date is older than today then tell me. I don't know why I am getting strange results with my code. Is there a shorter way to check this?

Code:
strExpDate = "8/11/2015"
 
do until objTextFile.AtEndOfStream
  strLine = objTextFile.ReadLine()
  If InStr(strLine, strExpDate) <> 0 then
  If strExpDate < Date Then
  MsgBox "Out Of Date" 
  Else
  MsgBox "Date is OK"
  End If
  End If
Loop
 
to be sure, convert the string which contains the valis date into a DATE-datatype
strExpDate = cdate("8/11/2015")
 
Thanks mikrom that did the trick. Here is my entire code and the only problem I am having now is how to detect if there is no date or the text file is empty so I can trigger the inputbox to enter a new date. I have tried the 'getfile' and 'filesize' but nothing seems to work when the file is empty.

Code:
 Const ForReading = 1, ForWriting = 2, ForAppending = 8, CreateIfNeeded = True

 Dim strExpDate, strLine, objFSO, objTextFile, expNewDate, objTextFile2
 
 Set WshShell = CreateObject("WScript.Shell")
 strDir = WshShell.CurrentDirectory & "\"

 Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set objTextFile = objFSO.OpenTextFile(strDir & "550189-01_Expiration.txt", ForReading)
 
do until objTextFile.AtEndOfStream
  strExpDate = objTextFile.ReadLine()
  strExpDate = CDate(strExpDate)
  If strExpDate <= Date Then
  MsgBox "SMT Adhesive 550189-01 Expired:   " & strExpDate, vbExclamation
  Call enterNewDate
 End If
loop

objTextFile.Close

Sub enterNewDate()
 Do
  newExpDate = InputBox("DATE HAS EXPIRED FOR: 550189-01" & vbCrlf & vbcrlf & vbCrlf & vbcrlf & _
  "ENTER NEW EXPIRATION DATE:", "550189-01", "MM/DD/YYYY")
  Loop Until IsDate(newExpDate)
  
  newExpDate = CDate(newExpDate)
  
  If newExpDate > Date Then
  Set objTextFile2 = objFSO.OpenTextFile(strDir & "550189-01_Expiration.txt", 2)
  objTextFile2.Write newExpDate
  objTextFile2.Close
  Else
  MsgBox "Date should be greater than today!", vbCritical, "Entry Error"
  Call enterNewDate
  End If
End Sub
 
I don't know what could your file contain. Only one date or sonething more ?
 
Yes, my file only contains one line with a date. Based on what guitarzan says, I don't know if I should be checking for a date or empty file. If it's empty I want to call my Sub. Unless that's not the best way to go about it.
 
I would read the file and if the date is not in the file the I would use InputBox.
Here the quicky example:
Code:
[COLOR=#0000ff]'create object[/color]
[COLOR=#804040][b]set[/b][/color] oFSO [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]CreateObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Scripting.FileSystemObject"[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#0000ff]'open the input file[/color]
[COLOR=#804040][b]set[/b][/color] oInFile [COLOR=#804040][b]=[/b][/color] oFSO[COLOR=#804040][b].[/b][/color]OpenTextFile[COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"expiration.txt"[/color][COLOR=#804040][b])[/b][/color]

str_input [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]""[/color]
[COLOR=#0000ff]'for each line in the input file[/color]
[COLOR=#804040][b]do[/b][/color] [COLOR=#804040][b]while[/b][/color] [COLOR=#804040][b]not[/b][/color] oInFile[COLOR=#804040][b].[/b][/color]AtEndOfStream
 [COLOR=#0000ff] 'read the line[/color]
  str_input [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]trim[/color][COLOR=#804040][b]([/b][/color]oInFile[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]ReadLine[/color][COLOR=#804040][b]())[/b][/color]
  Wscript[COLOR=#804040][b].[/b][/color]echo str_input
 [COLOR=#0000ff] ' if date found then exit the loop[/color]
  [COLOR=#804040][b]if[/b][/color] [COLOR=#008080]isDate[/color][COLOR=#804040][b]([/b][/color]str_input[COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]then[/b][/color]
    WScript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"Date in file found: '"[/color] [COLOR=#804040][b]&[/b][/color] str_input [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"'"[/color]
    [COLOR=#804040][b]exit[/b][/color] [COLOR=#804040][b]do[/b][/color]
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]  
[COLOR=#804040][b]loop[/b][/color]

[COLOR=#0000ff]'close the input file[/color]
oInFile[COLOR=#804040][b].[/b][/color][COLOR=#804040][b]close[/b][/color]
[COLOR=#0000ff]'release object from memory[/color]
[COLOR=#804040][b]set[/b][/color] oFSO [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]nothing[/b][/color]

[COLOR=#0000ff]' if the file doen't contain date then use the InputBox[/color]
[COLOR=#804040][b]do[/b][/color] [COLOR=#804040][b]while[/b][/color] [COLOR=#804040][b]not[/b][/color] [COLOR=#008080]isDate[/color][COLOR=#804040][b]([/b][/color]str_input[COLOR=#804040][b])[/b][/color] 
  str_input[COLOR=#804040][b]=[/b][/color][COLOR=#008080]InputBox[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Enter Expiration Date:"[/color][COLOR=#804040][b],[/b][/color][COLOR=#ff00ff]"Date"[/color][COLOR=#804040][b])[/b][/color]
[COLOR=#804040][b]loop[/b][/color]

[COLOR=#0000ff]' to be sure convert to DATE-format[/color]
exp_date[COLOR=#804040][b]=[/b][/color][COLOR=#008080]cdate[/color][COLOR=#804040][b]([/b][/color]str_input[COLOR=#804040][b])[/b][/color]
WScript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"Expiration Date found: '"[/color] [COLOR=#804040][b]&[/b][/color] exp_date [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"'"[/color]

[COLOR=#0000ff]' process date[/color]
[COLOR=#0000ff]' ...[/color]
 
mikrom... thanks for your time and help on this, it is greatly appreciated. I will use this and modify it to my needs. I did get one version working but the way I wrote the code is quite a bit different so I will use this for learning. Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top