Solved

FIND SPECIFIC STRINGS IN A TEXT FILE

Posted on 2006-06-21
4
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…

690 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