Solved

Need help with file parsing

Posted on 2007-03-20
13
201 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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.
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

773 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