Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

FIND SPECIFIC STRINGS IN A TEXT FILE

Posted on 2006-06-21
4
Medium Priority
?
189 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 2000 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

609 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