?
Solved

Editing text files with put-get

Posted on 2003-03-25
5
Medium Priority
?
233 Views
Last Modified: 2010-05-01
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
Comment
Question by:RickAtCanadadotcom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 11

Expert Comment

by:supunr
ID: 8207318
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
 
LVL 11

Expert Comment

by:supunr
ID: 8207352
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
 
LVL 11

Expert Comment

by:supunr
ID: 8207377
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
 
LVL 1

Accepted Solution

by:
robertlees earned 300 total points
ID: 8207386
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
 
LVL 11

Expert Comment

by:supunr
ID: 8207435
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

777 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