Solved

convert and edit strings from a text file within a FlexGrid - VB6

Posted on 2014-11-25
18
101 Views
Last Modified: 2014-11-26
My objective is to manipulate a text file from my flexgrid  (MSFlexGrid1)   Then save it back to the same text file.

Attached is a text file which contains a list of websites.  These sites are written in a differently coded format starting with ^(.*\.)?    For example, google.com is written in the following format.

 ^(.*\.)?   thewebsitename   \   .com   $  

I would like to extract from this text file all these websites into the MSFlexGrid1.  The first column in the grid would list the sites in a readable format.  ex.  google.com

From the Flex Grid, I would modify the site name, for example to:  googleusercontent.com

After modifying in the FlexGrid, I would need to copy it back into the text file in the converted format:   ^(.*\.)?googleusercontent\.com$

Is there a way to disseminate the site names in this text file, separating these by sections, edit the website name in the flexgrid and then put it back together in the text file?

Any help in this regard would be very much appreciated.
allowed.txt
0
Comment
Question by:al4629740
  • 12
  • 6
18 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40466112
Let me see what I can do.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40466127
With the entry

^(.*\.)?facebook\.(com|net)$

do you want the result to be facebook.com or facebook.net?
0
 

Author Comment

by:al4629740
ID: 40466142
Wow I didn't notice that one.  Can it be corrected and made into two?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 46

Expert Comment

by:Martin Liss
ID: 40466147
This does the writing to the grid part. Change the file path and then give it a try and let me know if anything is wrong.

Dim FF As Integer
Dim strLine As String
Dim strParts() As String
Dim intPos As Integer
Dim intRow As Integer

FF = FreeFile

Open "C:\temp\allowed.txt" For Input As FF
    Do While Not EOF(FF)
        Line Input #FF, strLine
        If Left$(strLine, 1) = "^" Then
            strParts = Split(strLine, "?")
            If UBound(strParts) > 0 Then
                strParts(1) = Replace(strParts(1), "$", "")
                strParts(1) = Replace(strParts(1), "\", "")
                strParts(1) = Replace(strParts(1), "(com|net)", "com")
                ' look for the "." that's second from the end
                intPos = InStrRev(strParts(1), ".", Len(strParts(1)) - 5)
                intRow = intRow + 1
                If intRow = MSFlexGrid1.Rows Then
                    MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1
                End If
                MSFlexGrid1.TextMatrix(intRow, 1) = Mid$(strParts(1), intPos + 1)
            End If
        End If
    Loop

Close

Open in new window

0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40466149
Sure, but give the above a try first.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40466163
Just in case you can't get back to me before I go to sleep, I added code for the "(com|net)" situation.
Dim FF As Integer
Dim strLine As String
Dim strParts() As String
Dim intPos As Integer
Dim intRow As Integer
Dim bComNetFound As Boolean
Dim intLenBefore As Integer

FF = FreeFile

Open "C:\temp\allowed.txt" For Input As FF
    Do While Not EOF(FF)
        Line Input #FF, strLine
        If Left$(strLine, 1) = "^" Then
            strParts = Split(strLine, "?")
            If UBound(strParts) > 0 Then
                strParts(1) = Replace(strParts(1), "$", "")
                strParts(1) = Replace(strParts(1), "\", "")
                intLenBefore = Len(strParts(1))
                strParts(1) = Replace(strParts(1), "(com|net)", "com")
                bComNetFound = False
                ' If there was "(com|net)" in the entry we've
                ' found a case where we need to create grid entries
                If intLenBefore > Len(strParts(1)) Then
                    ' Set the flag for the special processing
                    bComNetFound = True
                End If
                ' look for the "." that's second from the end
                intPos = InStrRev(strParts(1), ".", Len(strParts(1)) - 5)
                intRow = intRow + 1
                If intRow = MSFlexGrid1.Rows Then
                    MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1
                End If
                MSFlexGrid1.TextMatrix(intRow, 1) = Mid$(strParts(1), intPos + 1)
                If bComNetFound Then
                    intRow = intRow + 1
                    strParts(1) = Replace(strParts(1), ".com", ".net")
                    MSFlexGrid1.TextMatrix(intRow, 1) = Mid$(strParts(1), intPos + 1)
                End If
            End If
        End If
    Loop
Close

Open in new window

0
 

Author Comment

by:al4629740
ID: 40466186
The first code works like a champ.  

However addressing the "(com|net)" situation in the second code leads to an error on line 38

Error is "subscript out of range"

Thanks for getting me up to this point
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40466211
This should fix it. The existing code handled the case where a new row needed to be added to the grid for the "regular" data but I neglected to consider the case where when "(com|net)" was found and two rows needed to be added. I'll check back with you in the morning.

Dim FF As Integer
Dim strLine As String
Dim strParts() As String
Dim intPos As Integer
Dim intRow As Integer
Dim bComNetFound As Boolean
Dim intLenBefore As Integer

FF = FreeFile

Open "C:\temp\allowed.txt" For Input As FF
    Do While Not EOF(FF)
        Line Input #FF, strLine
        If Left$(strLine, 1) = "^" Then
            strParts = Split(strLine, "?")
            If UBound(strParts) > 0 Then
                strParts(1) = Replace(strParts(1), "$", "")
                strParts(1) = Replace(strParts(1), "\", "")
                intLenBefore = Len(strParts(1))
                strParts(1) = Replace(strParts(1), "(com|net)", "com")
                bComNetFound = False
                ' If there was "(com|net)" in the entry we've
                ' found a case where we need to create grid entries
                If intLenBefore > Len(strParts(1)) Then
                    ' Set the flag for the special processing
                    bComNetFound = True
                End If
                ' look for the "." that's second from the end
                intPos = InStrRev(strParts(1), ".", Len(strParts(1)) - 5)
                intRow = intRow + 1
                If intRow = MSFlexGrid1.Rows Then
                    MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1
                End If
                MSFlexGrid1.TextMatrix(intRow, 1) = Mid$(strParts(1), intPos + 1)
                If bComNetFound Then
                    intRow = intRow + 1
                    If intRow = MSFlexGrid1.Rows Then
                        MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1
                    End If
                    strParts(1) = Replace(strParts(1), ".com", ".net")
                    MSFlexGrid1.TextMatrix(intRow, 1) = Mid$(strParts(1), intPos + 1)
                End If
            End If
        End If
    Loop
Close

Open in new window

0
 

Author Comment

by:al4629740
ID: 40466216
Yes indeed.  that was the fix.  Great job!  Part 1 is complete.  Get some rest.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40466958
Ok let me ask you a few questions about the rest of this process.

1.    How do you want to go about changing the website name? Would double-clicking the cell and then being able to edit it be acceptable?
2.    How do you want to handle the "(com|net)" entries since we now have one .com and one .net?
3.    Do you want all the lines from the file including lines like ;ALLOW_FACEBOOK_r02-31-2013 and ^fb\.me$ written back to the file?
4.    If the answer to that question is yes then that will be tricky but doable, but a much easier solution would be to just display every line in the file in the grid as is (in other words unchanged) and then allow you to change any part of a cell you wanted to.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40467694
While I was waiting I went ahead and assumed that your answers to #1 and #3 above was "yes" and you can ignore #4. With that in mind try the attached project. When you "Export" it creates a backup file called "allowed_saved.txt" of the original file in the project folder. Note that it ignores the second entry of any "(com|net)" pairs. I can make changes depending on your answer to #2. One answer to that question could be "don't create two entries" which would be the right answer if you would otherwise always what to make the same name change to both the .com and .net  members of the pair.
Q-28569730.zip
0
 

Author Comment

by:al4629740
ID: 40467763
Martin, just got home and trying it out soon.

Just to answer your questions

1.    How do you want to go about changing the website name? Would double-clicking the cell and then being able to edit it be acceptable?  Yes

2.    How do you want to handle the "(com|net)" entries since we now have one .com and one .net?
It could be listed at two separate entries on the next process.  Essentially this would correct the "(com|net)" entries which I'm not crazy about.

3.    Do you want all the lines from the file including lines like ;ALLOW_FACEBOOK_r02-31-2013 and ^fb\.me$ written back to the file?

Displaying every line that begins with ^ is the most important

4.    If the answer to that question is yes then that will be tricky but doable, but a much easier solution would be to just display every line in the file in the grid as is (in other words unchanged) and then allow you to change any part of a cell you wanted to.
0
 

Author Comment

by:al4629740
ID: 40467776
Wow Martin.  I am going to fully test this tonight, but it looks really good.....
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40467805
Remember that it currently doesn't fully handle the ^(.*\.)?facebook\.(com|net)$ entry. I assume from your comments that you want to see both facebook.com and facebook.net in the grid (which it does now) and you want to write the entries back to the file as
^(.*\.)?facebook\ .com$
and
^(.*\.)?facebook\ .net$

which it currently isn't doing,  where either one or both could have "facebook" changed to something else by you.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40467822
Oops. I just noticed that I didn't actually create the backup file.
0
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40467907
Okay, once again I assume "yes", this time  to post ID: 40467805. This version
makes that change,
assumes all file are in the application folder, and
actually writes the backup file.
Q-28569730-2.zip
0
 

Author Closing Comment

by:al4629740
ID: 40468035
You assumed correctly...

Martin, this is a very thorough and comprehensive solution to my original question.   I have follow up questions, but I will ask them in another question since they encompass other elements.

Thank you again
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40468136
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

786 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