Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Editing text files with put-get

Hi,

I want to edit a text file and strip out blocks of code, but all I can do is create a new file. I don't want a new file, I want to edit the one I have.

VB6 has an example in dealing with records using Put and Get but is no good to me.

I want to go from this:
<html>
<script language="javascript" blah blah..
         code here
</script>
<script language="javascript" blah blah..
         more code here
</script>
<body>
   this is OK
</body>
</html>

To this:
<html>
<body>
   this is OK
</body>
</html>

This is my code:
Dim textline
Open "C:\test.htm" For Input As #1    ' Open file.
Do While Not EOF(1)    ' Loop until end of file.
    'Line Input #1, textline    ' Read the line.
    Line Input #1, textline  ' Read the line.
    If Left(Trim(textline), 7) = "<script" Then
        Line Input #1, textline
        ' keep skipping until you see the closing tag.
        Do While Left(Trim(textline), 8) <> "</script"
            Line Input #1, textline
        Loop
    ElseIf Left(Trim(textline), 10) = "<noscript>" Then
        Line Input #1, textline
        ' keep skipping untile you see the closing tag.
        Do While Left(Trim(textline), 11) <> "</noscript>"
            Line Input #1, textline
        Loop
    Else
        Debug.Print textline
    End If
Loop
Close #1    ' Close file.

Any direction would be much appreciated.
0
RickAtCanadadotcom
Asked:
RickAtCanadadotcom
  • 4
1 Solution
 
supunrCommented:
how about using a RichTextbox to do the hardwork.  Here is a sample code.

Private Sub StripCode()
    Dim FileName As String
    Dim StartScriptAt As Long
    Dim EndScriptAt As Long
   
    With RichTextBox1
        FileName = "C:\Test.txt"
       
        .LoadFile FileName, rtfText
        StartScriptAt = 0
       
        Do
            StartScriptAt = .Find("<script")
            If (StartScriptAt <= 0) Then Exit Do
            EndScriptAt = .Find("</script")
            ' end script command not found, delete till end of the file
            If (EndScriptAt <= 0) Then
                EndScriptAt = Len(.Text)
            End If
            .SelStart = StartScriptAt
            .SelLength = EndScriptAt - StartScriptAt + 8
            .SelText = 0
        Loop
       
        .SaveFile FileName, rtfText
    End With
End Sub

Good Luck!
0
 
supunrCommented:
little modification to your program....

Dim textline as string
Dim ignoreLines as boolean

Open "C:\test.htm" For Input As #1    ' Open file.
ignoreLines = false
Do While Not EOF(1)    ' Loop until end of file.
ReadNextLine:
   Line Input #1, textline  ' Read the line.
   If (LCase(Left(Trim(textline), 7)) = "<script") Then
       ignoreLines = true
   Elseif (LCase(Left(Trim(textline), 8)) = "</script") then
       ignoreLines = false
   ElseIf (LCase(Left(Trim(textline), 10)) = "<noscript>") Then
       goto ReadNextLine
   ElseIf (LCase(Left(Trim(textline), 10)) = "</noscript>") Then
       goto ReadNextLine
   End If
   if (Not ignoreLines)
      debug.Print textLine
   end if
Loop
Close #1    ' Close file.
0
 
supunrCommented:
again....

Private Sub RemoveCodes(FileName as String)
    On Error goto RemErr
    Dim textline As String
    Dim ignoreLines As Boolean
   
'    Open "C:\test.htm" For Input As #1    ' Open file.
    Open FileName For Input As #1    ' Open file.

    ignoreLines = False
    Do While Not EOF(1)    ' Loop until end of file.
ReadNextLine:
        Line Input #1, textline  ' Read the line.
        If (LCase(Left(Trim(textline), 7)) = "<script") Then
            ignoreLines = True
            GoTo ReadNextLine
        ElseIf (LCase(Left(Trim(textline), 8)) = "</script") Then
            ignoreLines = False
            GoTo ReadNextLine
        ElseIf (LCase(Left(Trim(textline), 10)) = "<noscript>") Then
            GoTo ReadNextLine
        ElseIf (LCase(Left(Trim(textline), 10)) = "</noscript>") Then
            GoTo ReadNextLine
        End If
        If (Not ignoreLines) Then
            Debug.Print textline
        End If
    Loop
    Close #1    ' Close file.
    Exit Sub

RemErr:
    msgbox Err.Number & ": " & err.Description
    Reset ' close any open files
    On Error goto 0
End Sub

Good Luck!
0
 
robertleesCommented:
I would avoid reading and writing to the same file, even if it were possible. The following code creates a new file with just the bits of the original file that are required, then the original file is renamed with a .bak extension, and the new file is renamed to have the same name as the original file. The effect is the same, the code is more straightforward, and you keep a backup.

Sub Strip(ByVal strInFile As String)
    Dim strRecord As String
    Dim bolIgnore As Boolean
   
    Open strInFile For Input As #1
    Open strInFile & ".new" For Output As #2
   
    Do Until EOF(1)
        Line Input #1, strRecord
        If Left(strRecord, 7) = "<script" Or Left(strRecord, 10) = "<noscript>" Then
            bolIgnore = True
        ElseIf Left(strRecord, 8) = "</script" Or Left(strRecord, 11) = "</noscript>" Then
            bolIgnore = False
        ElseIf Not bolIgnore Then
            Print #2, strRecord
        End If
    Loop
   
    Close #1
    Close #2
   
    If Dir(strInFile & ".bak") <> "" Then
        Kill strInFile & ".bak"
    End If
    Name strInFile As strInFile & ".bak"
   
    Name strInFile & ".new" As strInFile
End Sub
0
 
supunrCommented:
K, I got your question now....another updated version....

Private Sub RemoveCodes(FileName as String)
   On Error goto RemErr
   Dim textline As String
   Dim ignoreLines As Boolean
   
'    Open "C:\test.htm" For Input As #1    ' Open file.
   Open FileName For Input As #1    ' Open file.
   Open FileName & ".Bak" For Output As #2    ' Open file.

   ignoreLines = False
   Do While Not EOF(1)    ' Loop until end of file.
ReadNextLine:
       Line Input #1, textline  ' Read the line.
       If (LCase(Left(Trim(textline), 7)) = "<script") Then
           ignoreLines = True
           GoTo ReadNextLine
       ElseIf (LCase(Left(Trim(textline), 8)) = "</script") Then
           ignoreLines = False
           GoTo ReadNextLine
       ElseIf (LCase(Left(Trim(textline), 10)) = "<noscript>") Then
           GoTo ReadNextLine
       ElseIf (LCase(Left(Trim(textline), 10)) = "</noscript>") Then
           GoTo ReadNextLine
       End If
       If (Not ignoreLines) Then
           Print #2, textline
       End If
   Loop
   Close #1    ' Close file.
   Close #2    ' Close file.
   if (Dir(FileName & ".bak") <> "") then
       Kill FileName
       Name FileName & ".bak" As FileName
   end if

   Exit Sub

RemErr:
   msgbox Err.Number & ": " & err.Description
   Reset ' close any open files
   On Error goto 0
End Sub
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now