Re: Return file pointer position

From: McKirahan (News_at_McKirahan.com)
Date: 12/07/04


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



Relevant Pages

  • Re: Modify a .doc document with a macro without Word ?
    ... Const LL_ITEMPTR = 4 ' Pointer to the item ... Public Sub Add(Item As Variant, Optional Key As String, Optional Before As Variant, Optional After As Variant) ... Dim lpArray As Long ' Pointer to the first element of the array ... Dim lpItem As Long ' Pointer to the item ...
    (microsoft.public.scripting.vbscript)
  • RE: runtime error 3075 using sql insert query where data has single qu
    ... Call the ReplaceStr function in the error handler of your existing code. ... Dim rs As DAO.Recordset ... Dim WorkText As String ... Dim Pointer As Integer ...
    (microsoft.public.access.modulesdaovba)
  • Problems with calling avifil32.dll function in vb.net
    ... application which converts BMP Images to AVI. ... This function wants to receive a pointer to a pointer to an ... to a pointer to unmanaged memory. ... Dim oGC As GCHandle = GCHandle.Alloc ...
    (microsoft.public.vb.winapi.graphics)
  • RE: Does .Net modify native IDataObject within an Application?
    ... hands on the RCW wrapped IDataObject. ... support Drag and Drop of files, folders, etc using the IShellFolder ... Dim dataObjectPtr As IntPtr ... example) stg.hGlobal is a pointer TO A pointer which, in turn, points to a ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Before Double Click
    ... I was confusing what it means to pass an object by a pointer ... Dim rngVal As Range ... Sub EFG(ByVal r1 As Range, ByRef r2 As Range) ...
    (microsoft.public.excel.programming)