Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need help with file parsing

Posted on 2007-03-20
13
Medium Priority
?
241 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

972 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