bernie1774
asked on
Removing comments (#) from text file
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
# 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
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
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
ASKER
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
##begin
program_code_is_here #comment
would be output to the final text file as
program_code_is_here
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Run mu code with the sample code above
S
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
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
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
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
ASKER
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
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)
...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)
Idle :) you didn't see my correction were I did exactly what you just posted.
S
S
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
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
Well, the example of the file above states a comment at the end of each and every line.
S
S
What do you want to do with the preceding lines, such as "# change the information" do you want to ignore them altogether?
S