Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 109
  • Last Modified:

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
0
al4629740
Asked:
al4629740
  • 12
  • 6
1 Solution
 
Martin LissRetired ProgrammerCommented:
Let me see what I can do.
0
 
Martin LissRetired ProgrammerCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Martin LissRetired ProgrammerCommented:
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 LissRetired ProgrammerCommented:
Sure, but give the above a try first.
0
 
Martin LissRetired ProgrammerCommented:
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 LissRetired ProgrammerCommented:
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 LissRetired ProgrammerCommented:
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 LissRetired ProgrammerCommented:
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 LissRetired ProgrammerCommented:
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 LissRetired ProgrammerCommented:
Oops. I just noticed that I didn't actually create the backup file.
0
 
Martin LissRetired ProgrammerCommented:
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
 
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 LissRetired ProgrammerCommented:
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

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.

  • 12
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now