Solved

FIND SPECIFIC STRINGS IN A TEXT FILE

Posted on 2006-06-21
4
182 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

912 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

15 Experts available now in Live!

Get 1:1 Help Now