Solved

Need help with file parsing

Posted on 2007-03-20
13
199 Views
Last Modified: 2013-11-25
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
Comment
Question by:leotron
  • 6
  • 6
13 Comments
 
LVL 27

Expert Comment

by:VBRocks
ID: 18760494
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
 
LVL 1

Author Comment

by:leotron
ID: 18760510
I have tried that but it continues to do the same thing. Thank you though
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18760599
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
 
LVL 1

Author Comment

by:leotron
ID: 18760639
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18760648
Try modifying the compare...

 If Left(strLn, 1) <> "#" And Trim(strLn) <> "" Then
0
 
LVL 1

Author Comment

by:leotron
ID: 18760661
Nope same thing
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18760678
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
 
LVL 1

Author Comment

by:leotron
ID: 18760698
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18760717
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
 
LVL 1

Author Comment

by:leotron
ID: 18760733
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18760751
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18760764
Happy to help - thanx for the grade! :^)
0
 
LVL 1

Author Comment

by:leotron
ID: 18760767
You earned it thank you for the help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now