Link to home
Start Free TrialLog in
Avatar of SAbboushi
SAbboushiFlag for United States of America

asked on

Text File as a string?

Hi-

I am using the code (below) to read a textfile into a variable called sDetailFormScript.  There are 2 strings buried within the text file and I want extract everything between those two strings (and truncate everything before the first string occurence and truncate everything after the second string occurence).  I was looking for something as simple as an InStr function to identify the starting and ending character positions within the text file (the strings are unique by definition).  However, I believe InStr will not tolerate a vbCRLF as a character within a string(?) - and since this is a text file, I suspect that InStr will only function on a line by line basis (I am assuming here).

My question: is there a way for me to treat a text file as a string (instead of multiple lines) so that I can do string manipulation on the file using character positions?  (One thought is converting the vbCRLF characters within sDetailFormScript - if they are indeed the obstacle to using InStr - to some unique string, then do the string manipulations, then convert those unique strings back to vbCRLF).

I was trying to avoid processing the text file one row at a time... so I would appreciate if anyone has ideas on how I can accomplish what I have described.

The code I am using to load the text file as a string into sDetailFormScript:

filename = "c:\sample.txt"
sDetailFormScript = FileText( filename)

Function FileText(ByVal filename As String) As String
'UPDATE: Andrew Marshall wrote us to point out that the above routine fails when the file includes a Ctrl-Z (EOF) character, so we prepared a better version that works around that problem:
   
    Dim handle As Integer
   
    ' ensure that the file exists
    If Len(Dir$(filename)) = 0 Then
        Err.Raise 53   ' File not found
    End If
   
    ' open in binary mode
    handle = FreeFile
    Open filename$ For Binary As #handle
    ' read the string and close the file
    FileText = Space$(LOF(handle))
    Get #handle, , FileText
    Close #handle
End Function



With Regards-
Sam
ASKER CERTIFIED SOLUTION
Avatar of Dabas
Dabas
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dim handle As Integer
Dim tmpString  as string

    ' ensure that the file exists
    If Len(Dir$(filename)) = 0 Then
        Err.Raise 53   ' File not found
    End If
   
    ' open in binary mode
    handle = FreeFile


    Open filename$ For Binary As #handle 'OR Use, Open filename$ For Input As #handle

       Do While Not EOF(handle)
        tmpString = ""
     
        Line Input #handle, tmpString
        FileText= FileText & tmpString
      Loop

    Close #handle


You should get all the string content of the file in the 'FileText' String variable. thechar position should remain same as the char position in file.


Avatar of SAbboushi

ASKER

Thanks Dabas

I actually did some testing before my post - it's just that I made a mistake in my test  ; )

Thanks for getting me straightened out!

With Regards-
Sam
raahgeer

Thanks for your post - Dabas was able to point out that I was mistaken in my assumptions.

With Regards-
Sam