(Solved) – VBA to move text renaming to error directory in case of error

  • by

I have a code below which renames tekst files on content. The data could have some doublings, so i want to move the txt files to an error directory in case of an error. Can someone help me?

Sub RenameTextFile()

    Const SpecialCharacters As String = ",/,:,*,?,<,>,|,""," ' Modify this as neccesary
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fso, MyFile, FileName, TextLine
    Dim char As Variant

    Set fso = CreateObject("Scripting.FileSystemObject")

    Set fol = fso.GetFolder("C:Research syntheses - Meta analysisTxt files ECSout")

     For Each fil In fol.Files

        FileName = fil

        Set MyFile = fso.OpenTextFile(FileName, ForReading)

        Do While MyFile.AtEndOfStream <> True

                Dim tmpLine As String
                TextLine = MyFile.ReadLine
                tmpLine = RemoveWhiteSpace(TextLine)
                If Len(tmpLine) = 0 Then
                    TextLine = tmpLine
                End If
            Loop Until Len(TextLine) > 0

           For Each char In Split(SpecialCharacters, ",")
                TextLine = Replace(TextLine, char, "")


            fil.Name = TextLine & ".txt"
            Exit Do


    Next fil
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *