Solved

Extracting Portion of a file from text file

Posted on 2006-11-15
10
231 Views
Last Modified: 2010-04-30
I am no great programmer but I have a copy of visual basic 6 on my machine.  I have a text file one.reg (exported registry entry) but the file has a lot of hexadecimals, I need to extract only a portion of the file eg "0012902" which represents a student login name. There are many students login name in the file but they all fall between the quotes " ".  I need to extract all the idnumbers from this file to another new file on seperate lines. (eg line 1 0012902, line 2 120232).  Thanks in advance Raymond.
0
Comment
Question by:rwhittle
  • 4
  • 2
10 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 17949655
Can you post the file here or a sample of the file?

Kevin
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 17950212
Hi rwhittle,

I made a dummy text file, with contents:


be59g46a60d2883"1"a39a9bf26ab1fh
hbi40h89212bdfac"2"h115ahbf202g851i4d
5i8f37fg"3"af9522f067b1
9ed4ag4ccfh"4"1ff6fhh6h43h930e2
9724cea656gi161dd"5"g1g1i1a1d9896h6
8c38e97i056hdghc2bd"6"40d9iaif576dcachd
7b"7"i5ehh1f2db
h6"8"5ibhg4f6c29e8c93281e
ed"9"ga56f4
hg5eie72h"10"h7280g7e
7hc964hadd0hcf37"11"0d8
g5a056g9"12"638
64h4fgi8c3gbdbi9109e"13"hhf0c09183i4b540ce
b34aihf"14"9
1e"15"i55h85
e2g70"16"d3525d6d9bb6hiccda41
49g0h4di161i0"17"e2037hffc1
d7258g1b3h9c82gc0e9c"18"ag181h763
dgb2d8bif75f3"19"ebag6fe0ega0
81b5"20"hi0
0c0e21c830hc5i5"21"26adbcgca1316bd3igc4
486a1"22"8i300ighfbe9bhg
f"23"832ib1
hf9"24"9hfdd3hh4g3dd2bi8e5h
3a"25"1bb6faf



I then wrote this code:

' Function by Patrick Matthews

Option Explicit

Function RegExpFind(LookIn As String, PatternStr As String, Optional Pos, _
    Optional MatchCase As Boolean = True)

    ' This function uses Regular Expressions to parse a string (LookIn), and return matches to a
    ' pattern (PatternStr).  Use Pos to indicate which match you want:
    ' Pos omitted               : function returns a zero-based array of all matches
    ' Pos = 0                   : the last match
    ' Pos = 1                   : the first match
    ' Pos = 2                   : the second match
    ' Pos = <positive integer>  : the Nth match
    ' If Pos is greater than the number of matches, is negative, or is non-numeric, the function
    ' returns an empty string.  If no match is found, the function returns an empty string
   
    ' If MatchCase is omitted or True (default for RegExp) then the Pattern must match case (and
    ' thus you may have to use [a-zA-Z] instead of just [a-z] or [A-Z]).
   
    ' If you use this function in Excel, you can use range references for any of the arguments.
    ' If you use this in Excel and return the full array, make sure to set up the formula as an
    ' array formula.  If you need the array formula to go down a column, use TRANSPOSE()
   
    Dim RegX As Object
    Dim TheMatches As Object
    Dim Answer() As String
    Dim Counter As Long
   
    ' Evaluate Pos.  If it is there, it must be numeric and converted to Long
    If Not IsMissing(Pos) Then
        If Not IsNumeric(Pos) Then
            RegExpFind = ""
            Exit Function
        Else
            Pos = CLng(Pos)
        End If
    End If
   
    ' Create instance of RegExp object
    Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = True
        .IgnoreCase = Not MatchCase
    End With
       
    ' Test to see if there are any matches
    If RegX.test(LookIn) Then
       
        ' Run RegExp to get the matches, which are returned as a zero-based collection
        Set TheMatches = RegX.Execute(LookIn)
       
        ' If Pos is missing, user wants array of all matches.  Build it and assign it as the
        ' function's return value
        If IsMissing(Pos) Then
            ReDim Answer(0 To TheMatches.Count - 1) As String
            For Counter = 0 To UBound(Answer)
                Answer(Counter) = TheMatches(Counter)
            Next
            RegExpFind = Answer
       
        ' User wanted the Nth match (or last match, if Pos = 0).  Get the Nth value, if possible
        Else
            Select Case Pos
                Case 0                          ' Last match
                    RegExpFind = TheMatches(TheMatches.Count - 1)
                Case 1 To TheMatches.Count      ' Nth match
                    RegExpFind = TheMatches(Pos - 1)
                Case Else                       ' Invalid item number
                    RegExpFind = ""
            End Select
        End If
   
    ' If there are no matches, return empty string
    Else
        RegExpFind = ""
    End If
   
    ' Release object variables
    Set RegX = Nothing
    Set TheMatches = Nothing
   
End Function

Sub GrabData()

    Dim Entries As Variant
    Dim fso As Object
    Dim ts As Object
    Dim WholeFile As String
   
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.OpenTextFile("C:\Folder\Subfolder\blah.txt")
    WholeFile = ts.ReadAll
    ts.Close
    Set ts = Nothing
    Set fso = Nothing
   
    Entries = RegExpFind(WholeFile, """\d+""")
   
    [a:a].Clear
    [a1].Resize(UBound(Entries) + 1, 1).Value = Application.Transpose(Entries)
   
End Sub


GrabData successfully retrieved the data for me.

Regards,

Patrick
0
 

Author Comment

by:rwhittle
ID: 17950341
This is a portion of the file.  I would love to get out 0120049, 0120050, 0120052 from this file.

0120049$"=hex:01,00,04,80,ac,00,00,00,bc,00,00,00,00,00,00,00,14,00,00,00,02,\
  00,98,00,04,00,00,00,00,00,24,00,ff,01,1f,00,01,05,00,00,00,00,00,05,15,00,\
  00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,e3,04,00,00,00,00,24,00,ff,01,1f,\
  00,01,05,00,00,00,00,00,05,15,00,00,00,79,e3,fc,53,44,dd,b8,3d,23,5f,63,6b,\
  0e,05,00,00,00,00,24,00,ff,01,1f,00,01,05,00,00,00,00,00,05,15,00,00,00,79,\
  e3,fc,53,44,dd,b8,3d,23,5f,63,6b,0b,05,00,00,00,00,24,00,ff,01,1f,00,01,05,\
  00,00,00,00,00,05,15,00,00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,77,04,00,\
  00,01,02,00,00,00,00,00,05,20,00,00,00,20,02,00,00,01,05,00,00,00,00,00,05,\
  15,00,00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,01,02,00,00
"0120050$"=hex:01,00,04,80,ac,00,00,00,bc,00,00,00,00,00,00,00,14,00,00,00,02,\
  00,98,00,04,00,00,00,00,00,24,00,ff,01,1f,00,01,05,00,00,00,00,00,05,15,00,\
  00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,e4,04,00,00,00,00,24,00,ff,01,1f,\
  00,01,05,00,00,00,00,00,05,15,00,00,00,79,e3,fc,53,44,dd,b8,3d,23,5f,63,6b,\
  0e,05,00,00,00,00,24,00,ff,01,1f,00,01,05,00,00,00,00,00,05,15,00,00,00,79,\
  e3,fc,53,44,dd,b8,3d,23,5f,63,6b,0b,05,00,00,00,00,24,00,ff,01,1f,00,01,05,\
  00,00,00,00,00,05,15,00,00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,77,04,00,\
  00,01,02,00,00,00,00,00,05,20,00,00,00,20,02,00,00,01,05,00,00,00,00,00,05,\
  15,00,00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,01,02,00,00
"0120052$"=hex:01,00,04,80,ac,00,00,00,bc,00,00,00,00,00,00,00,14,00,00,00,02,\
  00,98,00,04,00,00,00,00,00,24,00,ff,01,1f,00,01,05,00,00,00,00,00,05,15,00,\
  00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,e5,04,00,00,00,00,24,00,ff,01,1f,\
  00,01,05,00,00,00,00,00,05,15,00,00,00,79,e3,fc,53,44,dd,b8,3d,23,5f,63,6b,\
  0e,05,00,00,00,00,24,00,ff,01,1f,00,01,05,00,00,00,00,00,05,15,00,00,00,79,\
  e3,fc,53,44,dd,b8,3d,23,5f,63,6b,0b,05,00,00,00,00,24,00,ff,01,1f,00,01,05,\
  00,00,00,00,00,05,15,00,00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,77,04,00,\
  00,01,02,00,00,00,00,00,05,20,00,00,00,20,02,00,00,01,05,00,00,00,00,00,05,\
  15,00,00,00,db,eb,0c,50,57,66,e2,62,82,8b,a6,28,01,02,00,00
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17950476
rwhittle,

Using that sample data, just change one line in GrabData:

    Entries = RegExpFind(WholeFile, "(^|"")\d+\$""")


Regards,

Patrick
0
 

Author Comment

by:rwhittle
ID: 17957007
I copied the code change the line run it using Visual Basic 6 it runs but nothing happens.  I am expecting the program to ask me or read the data file then extract the data to the  result file.  Await your response
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17957106
Can you upload a sample file to www.ee-stuff.com?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 18374415
Dan,

In my testing, my code successfully extracted the targeted strings.  In my opinion, I answered the
question.

Thank you for your cleanup efforts,

Patrick
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

837 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