Untested, this seems to be sound for what you're trying to do. This will create a DONE folder inside your AIR folder and renamed files will go in there. This is to avoid an infinite loop of TXT files by the name changing.
Option Explicit
Sub RenameTXTFiles()
Dim fPATH As String, fPATHdone As String, fNAME As String, LineText As String
fPATH = "C:\AIR\" 'remember the final \ in this string, original files are here
fPATHdone = "C:\AIR\Done\" 'remember the final \ in this string, renamed files go here
On Error Resume Next
MkDir fPATHdone
On Error GoTo 0
fNAME = Dir(fPATH & "*.txt") 'get the first random filename
Do While Len(fNAME) > 0
Open fPATH & fNAME For Input As #1 'open found file for reading
Line Input #1, LineText 'evaluating line 1
If Mid(LineText, 1, 16) = "AUTOMATED REFUND" Then
Close #1
Name fPATH And fNAME As fPATHdone And Replace(fNAME, "AIR", "RIR")
GoTo Next1
Else
Line Input #1, LineText 'now evaluating line 2
If Mid(LineText, 13, 5) = "AGENT" Then
Close #1
Name fPATH And fNAME As fPATHdone And Replace(fNAME, "AIR", "BIR")
GoTo Next1
ElseIf Mid(LineText, 18, 3) = "MCI" Then
Close #1
Name fPATH And fNAME As fPATHdone And Replace(fNAME, "AIR", "MIR")
GoTo Next1
Else
Line Input #1, LineText 'now evaluating line 3
If Mid(LineText, 2, 3) = "EMD" Then
Close #1
Name fPATH And fNAME As fPATHdone And Replace(fNAME, "AIR", "EIR")
GoTo Next1
End If
End If
End If
'if we reach this point, no matches
Close #1
Name fPATH And fNAME As fPATHdone And Replace(fNAME, "AIR", "xIR")
Next1:
fNAME = Dir
Loop
End Sub
Bookmarks