Avatar of al4629740
al4629740Flag for United States of America asked on

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
Visual Basic Classic

Avatar of undefined
Last Comment
Martin Liss

8/22/2022 - Mon
Martin Liss

Let me see what I can do.
Martin Liss

With the entry

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

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

Wow I didn't notice that one.  Can it be corrected and made into two?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Martin Liss

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

Martin Liss

Sure, but give the above a try first.
Martin Liss

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
al4629740

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
Martin Liss

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

ASKER
al4629740

Yes indeed.  that was the fix.  Great job!  Part 1 is complete.  Get some rest.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Martin Liss

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.
Martin Liss

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
ASKER
al4629740

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
al4629740

Wow Martin.  I am going to fully test this tonight, but it looks really good.....
Martin Liss

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.
Martin Liss

Oops. I just noticed that I didn't actually create the backup file.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Martin Liss

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
al4629740

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
Martin Liss

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