Solved

FIND SPECIFIC STRINGS IN A TEXT FILE

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

777 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