Solved

Removing comments (#) from text file

Posted on 2004-10-08
12
125 Views
Last Modified: 2012-05-05
I need to remove comments from a text file, the are all preceded by a # and followed by a carrage return, so basicly all i need to do is file and instance of the # symbol in a variable i've loaded and then delete everything up until it reaches a carrage return, then continue to look for other occurances of # and remove them...here is an example of the file

# change the information
      layer_select layer 11; select rowend; detail_edit 1 shade            ##item1
      layer_select layer 1; select rowend; detail_edit 1 shade      ## item2
      layer_select layer 3; select rowend; detail_edit 1 shade      ## item3

#begin section2

thanks guys
0
Comment
Question by:bernie1774
  • 7
  • 3
  • 2
12 Comments
 
LVL 19

Expert Comment

by:Shauli
ID: 12264354
Where do you want the results, in a new text file?
What do you want to do with the preceding lines, such as "# change the information"    do you want to ignore them altogether?

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12264437
If the answer to question 1 is yes, and to question 2 is no then:

Private Sub Command1_Click()
Dim myLine As String, mySplit() As String
Open "C:\Documents and Settings\Shauli.MOBILE\My Documents\test.txt" For Input As #1
    Open "C:\Documents and Settings\Shauli.MOBILE\My Documents\final.txt" For Output As #2
        Do Until EOF(1)
            Line Input #1, myLine
                mySplit = Split(myLine, "##", -1)
                If UBound(mySplit) > 0 Or Left(myLine, 1) = "#" Then
                    If mySplit(0) <> "" Then
                        Print #2, Trim(mySplit(0))
                    End If
                End If
        Loop
    Close #2
Close #1
End Sub

S
0
 

Author Comment

by:bernie1774
ID: 12264705
Yes the final results are to be output to a text file, but yes i want to ignore (and delete the comment lines all together).  so

##begin
program_code_is_here  #comment

would be output to the final text file as

program_code_is_here
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 12264742
<Please don't accept this as the answer>

Just some minor modifications to Shauli's code to handle the comments at the end of the lines as well:

Private Sub Command1_Click()
    Dim myLine As String
    Dim poundPos As Integer
    Open "C:\Documents and Settings\Shauli.MOBILE\My Documents\test.txt" For Input As #1
        Open "C:\Documents and Settings\Shauli.MOBILE\My Documents\final.txt" For Output As #2
            Do Until EOF(1)
                Line Input #1, myLine
                    poundPos = InStr(myLine, "#")
                    If poundPos > 0 Then
                        myLine = Trim(Left(myLine, poundPos - 1))
                    End If
                    If myLine <> "" Then
                        Print #2, Trim(myLine)
                    End If
            Loop
        Close #2
    Close #1
End Sub
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12264752
Idle, the comments at the end of the line are being taken care of by the split.

Run mu code with the sample code above

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12264755
If you want to get rid of those lines as well then:

Private Sub Command1_Click()
Dim myLine As String, mySplit() As String
Open "C:\Documents and Settings\Shauli.MOBILE\My Documents\test.txt" For Input As #1
    Open "C:\Documents and Settings\Shauli.MOBILE\My Documents\final.txt" For Output As #2
        Do Until EOF(1)
            Line Input #1, myLine
                mySplit = Split(myLine, "##", -1)
                If UBound(mySplit) > 0 Then
                    If mySplit(0) <> "" Then
                        Print #2, Trim(mySplit(0))
                    End If
                End If
        Loop
    Close #2
Close #1
End Sub

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12264758
Sorry, one correction:


Private Sub Command1_Click()
Dim myLine As String, mySplit() As String
Open "C:\Documents and Settings\Shauli.MOBILE\My Documents\test.txt" For Input As #1
    Open "C:\Documents and Settings\Shauli.MOBILE\My Documents\final.txt" For Output As #2
        Do Until EOF(1)
            Line Input #1, myLine
                mySplit = Split(myLine, "#", -1)
                If UBound(mySplit) > 0 Then
                    If mySplit(0) <> "" Then
                        Print #2, Trim(mySplit(0))
                    End If
                End If
        Loop
    Close #2
Close #1
End Sub

S
0
 

Author Comment

by:bernie1774
ID: 12264770
that last script is cutting out all but one section of the code for some reason, and also padding it with formatting...if you like i can send you the file i'm working with and you can try it yourself, just give me your email (you can email if you dont want to post it i'm bernie@nyclubkid.com ) thanks
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12264778
Did you try my version?

...or you can use this modified version of Shauli's last post:

Private Sub Command1_Click()
Dim myLine As String, mySplit() As String
Open "C:\Documents and Settings\Shauli.MOBILE\My Documents\test.txt" For Input As #1
    Open "C:\Documents and Settings\Shauli.MOBILE\My Documents\final.txt" For Output As #2
        Do Until EOF(1)
            Line Input #1, myLine
                mySplit = Split(myLine, "#", -1)
                If mySplit(0) <> "" Then
                    Print #2, Trim(mySplit(0))
                End If
        Loop
    Close #2
Close #1
End Sub

(Shauli, if there isn't a "#" in the string then Ubound = 0.  You were only outputting if Ubound >0)
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12264784
Idle :) you didn't see my correction were I did exactly what you just posted.

S

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12264802
Actually, I took out this conditional in your last submission:

    If UBound(mySplit) > 0 Then

becuase if there isn't a "#" in the string (meaning there was no comment on that line) then the statement

    mySplit = Split(myLine, "#", -1)

will result in an array with only one element at Index 0 and the conditional was only allowing the output if Ubound was greater than zero.

~IM
0
 
LVL 19

Expert Comment

by:Shauli
ID: 12264875
Well, the example of the file above states a comment at the end of each and every line.

S
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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 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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 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