Solved

FIND SPECIFIC STRINGS IN A TEXT FILE

Posted on 2006-06-21
4
181 Views
Last Modified: 2010-04-07
How can I read a file character by character if I don't know how the lines are ended.  I need to find specific strings.  Once I find them, I need the information following them.  Then I will need to continue reading the file until I find the next piece of information I need.

My file looks like this:

ISA/00/          /00/          /01/200091189ASN   /ZZ/PAJCANADA      /060620/1411/U/00401/401000763/0/P/>
GS/FA/200091189ASN/PAJCANADA/20060620/1411/304002530/X/004010VICSST/997/304003153AK1/SH/100473001AK2/856/0008AK5/AAK9/A/1/1/1SE/6/304003153
GE/1/304002530
IEA/1/401000763

The files not always have the same number of lines or amount of information, but the information I am looking for will always follow specific strings for example, I need the information following the 6th "/"  and then the information following  the first "X", etc.
0
Comment
Question by:jchionglou
  • 2
4 Comments
 
LVL 14

Expert Comment

by:dlwyatt82
ID: 16955149
It's sort of semantics, really, but I wouldn't bother trying to read the file one character at a time.  Instead, read the ENTIRE file into a String variable and then process the variable one character at a time with a loop and Mid functions:

'***************

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("test.txt", 1)

strContents = objFile.ReadAll
objFile.Close

For i = 0 To Len(strContents)
  ch = Mid(strContents, i, 1)
  ' Do stuff
Next
0
 
LVL 22

Accepted Solution

by:
danaseaman earned 500 total points
ID: 16955618
Try using Split:

Option Explicit

Private Sub Form_Load()
   Dim i As Long
   Dim vSplit() As String
   Dim strContents As String
   Dim objFSO
   Dim objFile
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objFile = objFSO.OpenTextFile(App.Path & "\test.txt", 1)
   
   strContents = objFile.ReadAll
   objFile.Close
   
   'Debug.Print strContents

   vSplit = Split(strContents, "/")
   'Data after 6th /
   Debug.Print vSplit(6)
   'Data after first X
   For i = 0 To UBound(vSplit)
      'Debug.Print i, vSplit(i)
      If vSplit(i) = "X" Then
         Debug.Print vSplit(i + 1)
      End If
   Next
   
End Sub
0
 

Author Comment

by:jchionglou
ID: 16962289
Thanks danaseaman.  It works perfectly.  Just one more detail.  Once I found the right position inside a file, how do I read the following, let's say 4 characters.
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 16964528
To read just 4 chars use:
Left(vSplit(6),4)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now