[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

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
0
SAbboushi
Asked:
SAbboushi
  • 2
1 Solution
 
DabasCommented:
Hi SAbboushi,
> However, I believe InStr will not tolerate a vbCRLF as a character
> within a string(?)
You believe wrong

Do a little experiment.
Start a new project (I am assuming this is VB6?)
In a new form create a mulitline textbox named Text1 and fill it with three lines of text:
Line1
Line2
Line3

Now if you add a new command button with the following code:

    Debug.Print InStr(Text1, "Line2")

It will correctly report 8. Which is the length of the first line (5 characters) + 2 characters for CR and LF


Dabas
0
 
raahgeerCommented:
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.


0
 
SAbboushiAuthor Commented:
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
0
 
SAbboushiAuthor Commented:
raahgeer

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

With Regards-
Sam
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now