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

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
al4629740Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Let me see what I can do.
0
Martin LissOlder than dirtCommented:
With the entry

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

do you want the result to be facebook.com or facebook.net?
0
al4629740Author Commented:
Wow I didn't notice that one.  Can it be corrected and made into two?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Martin LissOlder than dirtCommented:
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
Martin LissOlder than dirtCommented:
Sure, but give the above a try first.
0
Martin LissOlder than dirtCommented:
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
al4629740Author Commented:
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
Martin LissOlder than dirtCommented:
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
al4629740Author Commented:
Yes indeed.  that was the fix.  Great job!  Part 1 is complete.  Get some rest.
0
Martin LissOlder than dirtCommented:
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
Martin LissOlder than dirtCommented:
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
al4629740Author Commented:
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
al4629740Author Commented:
Wow Martin.  I am going to fully test this tonight, but it looks really good.....
0
Martin LissOlder than dirtCommented:
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
Martin LissOlder than dirtCommented:
Oops. I just noticed that I didn't actually create the backup file.
0
Martin LissOlder than dirtCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
al4629740Author Commented:
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
Martin LissOlder than dirtCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.