• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Need help with file parsing

I am parsing a configuration file, the file is structured with "#" in front of every line that is to be commented out. the file also contains blank lines. I have the code inside of a command button and when you click the button the file is parsed and displayed into a text box. The only things that should be displayed is everything that is not commented out (eg lines that start with a "#" and blank lines should not be displayed) This is all working so far, the one thing though is that the first line of my file has  36 spaces before the text, the rest of the lines are fine though. I am stuck now please check my code and help me find the problem. Thank you.

[Begin Code]

Private Sub Command3_Click()

' Declare variables to use
Dim strArray() As String, strNewArray() As String, strFinalArray() As String
Dim strLn As String, strStrip As String, temp As String
Dim num As Integer, Upper As Integer, Max As Integer

Open "myfile.txt" For Input As #1

strArray = Split(Input(LOF(1), 1), vbCrLf)

Upper = UBound(strArray)
ReDim strNewArray(Upper) As String

For num = 0 To Upper
   
    strLn = strArray(num)
    strStrip = Mid(strLn, 1, 1)
       
    If strStrip = "" Then
        blank$ = strStrip
       
    ElseIf strStrip = "#" Then
        hash$ = strStrip
   
    Else
        strNewArray(num) = strLn & vbCrLf
    End If
   
Next

Close #1

Text1.Text = Join(strNewArray)

End Sub

[end code]

Here is a sample of the file I am parsing.

[begin file]
# Commented line
#
# Commented line
# Commented line
#
# Commented line

good data

[end file]

here is a sample of my result from running the command

[begin result]

                                    some text
more text
and more
and more yet
ok thats it

[end of result]

I am bot sure what I am doing wrong or what I am missing. Thanks for all your help
0
leotron
Asked:
leotron
  • 6
  • 6
1 Solution
 
VBRocksCommented:
Would the Trim() function help you achieve your goal?  It will remove leading and trailing spaces.  Also, you can use LTrim() to remove only leading (left) spaces, and RTrim() to remove trailing (right) spaces.

strLn = Trim(strArray(num))

Hope that helps.
0
 
leotronAuthor Commented:
I have tried that but it continues to do the same thing. Thank you though
0
 
sirbountyCommented:
What about something like this...

Dim strArray() As String, strNewArray() As String, strFinalArray() As String
Dim strLn As String, strStrip As String, temp As String
Dim num As Integer, Upper As Integer, Max As Integer
Open "myfile.txt" For Input As #1
strArray = Split(Input(LOF(1), 1), vbCrLf)
Upper = UBound(strArray)
For num = 0 To Upper
    strLn = strArray(num)
    If Left(strLn, 1) <> "#" And strLn <> "" Then
        strData = strData & vbCrLf & strLn
    End If
Next
Close #1
Text1.Text = strData
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.

 
leotronAuthor Commented:
Ok this code will display the text justified to the left but the one thing is that the first line is blank and then from the next line after the text starts to display correctly. I want to get rid of the blank lines as well. here is what I mean

[sample]
                        <------ blank line
some txt
next txt
more txt

[end sample]
0
 
sirbountyCommented:
Try modifying the compare...

 If Left(strLn, 1) <> "#" And Trim(strLn) <> "" Then
0
 
leotronAuthor Commented:
Nope same thing
0
 
sirbountyCommented:
Oh wait!
The problem is here...
        strData = strData & vbCrLf & strLn

First pass through strData won't hold anything, so it becomes
<cr>
[Your data]

So, let's see - try stripping it off using:

Text1.Text = mid(strData,2)
0
 
leotronAuthor Commented:
Now that was great the only thing is that I had to add 3 instead of 2 because if not it will display a question mark symbol. But it is doing what I want it to do, Great job sirbounty. One last question how can I display a list of check boxes via a loop that will correspond to each line of the text file.
0
 
sirbountyCommented:
Are you trying to dynamically create a check box per line of data?
Been a while since I've done anything like that in VB...

Not really sure I'm satisfied with that solution anyway.

I don't know your data, but perhaps use the | character to build the string, like this:

strData = strData & "|" & strLn

and then clean it up at the end, like this: (trims off the first, then replaces all others with a <cr>)
Text1.Text = replace(mid(strData,2),"|",vbCrLF)
0
 
leotronAuthor Commented:
Yes one check box per line of data, this will be on a seperate form that I am using to delete entries in the file. So I will be reading the file in again, and then adding a check box before the line of text.  If the check box is checked the line will be deleted from the text file once the user clicks on the delete button. and that other suggestion worked great also thanks
0
 
sirbountyCommented:
I'm really not sure on the check box...
Found a couple of walkthroughs - perhaps this will help.
http://www.thescarms.com/vbasic/VB6Features.asp
http://www.codeguru.com/vb/controls/vb_activex/activex/article.php/c3499/
0
 
sirbountyCommented:
Happy to help - thanx for the grade! :^)
0
 
leotronAuthor Commented:
You earned it thank you for the help.
0
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.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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