Re: Return file pointer position
From: McKirahan (News_at_McKirahan.com)
Date: 12/07/04
- Next message: McKirahan: "Re: Help! WScript application error!"
- Previous message: Mattias: "How do I get the trendline equation from Excel to script?"
- In reply to: Raphael Goubet: "Return file pointer position"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 07 Dec 2004 10:30:38 GMT
"Raphael Goubet" <rgoubet@yahoo.fr> wrote in message
news:da59d264.0412070006.52581d84@posting.google.com...
> Hi,
>
> I'm wondering how I can retrieve the current position of the file
> pointer in a text stram.
>
> I'm writing a script where I have to insert text *before* some
> specific line in a text stream. Since I don't know in advance where
> that line is, I have to test them all until I find it; now, that
> implies I use the ReadLine method to test each line, which will put
> the file pointer *after* that line. That means that when I've found
> the line, the pointer is already one line too far.
>
> Since it's not possible to move the pointer backwards, the only way I
> see is to store the pointer position in a variable before I read the
> next line. This way, if the line is the one I'm looking for, I can
> close and reopen the text file and return to the desired position with
> the Skip method.
>
> But how do I retrieve that position? The best thing would be a
> function that returns a value I can directly feed to the Skip method
> to go back to that position from the start of the file.
>
> Alternatively, does anyone know a better method to handle this?
>
> Thanks!
>
> Raphaël
Use FSO's ReadAll() method and loop through the resulting array; the index
of the item will serve as your "pointer".
Option Explicit
'* Declare Constants
Const cVBS = "readall.vbs"
Const cOT1 = "file1.ext"
Const cOT2 = "file2.ext"
Const cOTF = "some specific line"
Const cNEW = "a new line"
'* Declare Variables
Dim arrOTF
Dim intOTF
Dim strOTF
'* Declare Objects
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOTF
'* Read File
Set objOTF = objFSO.OpenTextFile(cOT1,1)
strOTF = objOTF.ReadAll()
Set objOTF = Nothing
Set objOTF = objFSO.OpenTextFile(cOT2,2,true)
'* Write File
arrOTF = Split(strOTF,vbCrLf)
For intOTF = 0 To UBound(arrOTF)
strOTF = arrOTF(intOTF)
If InStr(strOTF,cOTF) > 0 Then
objOTF.WriteLine(cNEW)
End If
objOTF.WriteLine(strOTF)
Next
'* Destroy Objects
Set objOTF = Nothing
Set objFSO = Nothing
'* Done
MsgBox "Done!",vbInformation,cVBS
- Next message: McKirahan: "Re: Help! WScript application error!"
- Previous message: Mattias: "How do I get the trendline equation from Excel to script?"
- In reply to: Raphael Goubet: "Return file pointer position"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|