0

I have made a code for reading a txt file and delete a set of lines in that file and save it in another location. Can you help me out in modifying the code for reading multiple files present in a particular folder and then do the process on all files and save those files with the same name in a different folder .I have attached the code herewith.

Sub cleantext()
Dim lineOfText As String
Dim skipLines As Boolean
'Open files for writing
Open "C:\Users\INNAR1\Desktop\input_static_files\ebb_htr01h.UCBG" For Input As #1
Open "C:\Users\INNAR1\Desktop\output_static_files\ebb_htr01h.UCBG" For Output As #2




skipLine = False
Do Until EOF(1)
Line Input #1, lineOfText
If lineOfText = "dynamics" Then skipLines = True
If lineOfText = "end dynamics" Then skipLines = False
If Not skipLines And Not lineOfText = "end dynamics" Then Print #2, lineOfText
Loop
Close #1
Close #2
End Sub

Thanks in advance

  • 2
    [this](http://stackoverflow.com/questions/10380312/loop-through-files-in-a-folder-using-vba) will help. Did you even search for the solution before asking the question? – Scott Holtzman Mar 28 '16 at 13:17

1 Answers1

0

You can use Scott's suggestion to loop over files and call the following version to process any given file. You should add error handling.

Sub cleantext(ByVal InputFileName As String, ByVal OutputFileName As String)
    Dim lineOfText As String
    Dim skipLines As Boolean

    Dim iInput As Integer
    Dim iOutput As Integer




    'Open files for writing
    Open InputFileName For Input As #iInput
    Open OutputFileName For Output As #iOutput

    skipLine = False
    Do Until EOF(1)
        Line Input #iInput, lineOfText
        If lineOfText = "dynamics" Then skipLines = True
        If lineOfText = "end dynamics" Then skipLines = False
        If Not skipLines And Not lineOfText = "end dynamics" Then Print #iOutput, lineOfText
    Loop

    Close #iInput
    Close #iOutput
End Sub
MikeC
  • 960
  • 1
  • 7
  • 15