0

I'm trying to get the last line in a .HOL file. It will get the last line but every time you run the script, it stacks the previous lines.

Dim lastLine
Dim objFile
Dim objFSO

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Desktop\OUTLOOK.HOL", ForReading)

Do Until objFile.AtEndOfStream

  lastLine = objFile.ReadLine

Loop

objFile.Close

Wscript.Echo lastLine

What I'm trying to make is a script that gets rid of any date that is older than the current date then add a new date at the bottom of the .HOL file. The reason I need the last line is so I can take that date and add 2 weeks to it.

What the Echo Looks like Run 1                      Run 2
Date1                                               Date1         
                                                    Date2
Run 3                                      
Date1
Date2
Date3

And so forth if you get it. (Id post picture but I cant yet)
=========== .HOL File ======= 
[Test Days] 5
Test Day, 5/3/2013
Test Day, 5/17/2013
Test Day, 5/31/2013
Test Day, 6/14/2013
Test Day, 6/28/2013
JayBec
  • 45
  • 2
  • 10
  • What do you mean by "stacks the previous lines"? Also you can't simply append a date at the bottom of the file. `.HOL` files have a specific format. Simply appending a date would break that format. So please explain in more detail what you want to remove and what you want to add. An example would be helpful. – Ansgar Wiechers Jun 28 '13 at 20:47
  • The format is included. I just need that last date of the file to add 2 weeks. Lets say the second line (in the .HOL file) is Test Day, 1/1/2013 (first line is [Test Days] 50. The last line of the file is Test Day, 6/6/2013 So what I got so far is IF 2nd Line Date < todays date I delete that line (which works) then add a new date at the bottom of the .HOL file. So ill put in all my dates. Run the script. 1st run = 6/20/2012 (correct) 2nd run = 7/4/2013 and 6/20/2012 Every run it will have an additional date after (I call stacked) the last date. – JayBec Jun 28 '13 at 21:13

2 Answers2

1

The easy fix:

    dim x

    Do Until objFile.AtEndOfStream
      x = objFile.ReadLine
      If objFile.atEndOfStream Then
         lastLine = x
      End If
    Loop

The following function will add 2 weeks to 31 jan 2010, you just need to compose that from your line:

    DateAdd("ww",2,"31-Jan-10")

couple of edits.. its been a while since vbscript!

  • When the file already is at the end of the stream, there's nothing left to read, so your command will just block. – Ansgar Wiechers Jun 28 '13 at 20:41
  • 2
    This puts the script in an infinite loop. Why would you need another if to say once you get to the end of the file store the variable? Wouldn't already store the last line once the loop hits the end? Does the .AtEndOfStream run after the last line of a file or stop on the last line? Oh and thanks for the DateAdd!!! That worked better than what I had before – JayBec Jun 28 '13 at 21:17
  • It wont block, this one will only pass the last in the stream as the readline is the function that moves to the next row.. duh? –  Jun 28 '13 at 23:08
  • Thanks everyone for your help! This has been a wealth of information! Thank you. – JayBec Jun 30 '13 at 20:52
1

If you want to replace the last date in the file you could try something like this:

Set fso = CreateObject("Scripting.FileSystemObject")

text = Split(fso.OpenTextFile("C:\Desktop\OUTLOOK.HOL").ReadAll, vbNewLine)

Set f = fso.OpenTextFile(filename, 2)
For i = 0 To UBound(text)-1
  f.WriteLine text(i)
Next

lastline = Split(text(UBound(text)), ",")
newdate = DateAdd("ww", 2, lastline(1))
lastline(1) = Year(newdate) & "/" & Right("0" & Month(newdate), 2) _
                & "/" & Right("0" & Day(newdate), 2)
f.WriteLine Join(lastline, ",")

f.Close
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328