Sub RenameFiles()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim FolderPath As String, path, newpath As String, count, x As Integer
FolderPath = "C:\My report\Text Files"
path = FolderPath & "\*.txt"
Filename = Dir(path)
Do While Filename <> ""
count = count + 1
Filename = Dir()
Loop
For x = 1 To count
Application.StatusBar = "Renaiming " & x & " of " & count & " files..."
newpath = FolderPath & "\Text_" & x & ".txt"
i = 1
Workbooks.OpenText Filename:=newpath, Origin:= _
437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), _
Array(39, 1), Array(50, 1)), TrailingMinusNumbers:=True
MyName = Left(Range("A8"), 8)
shift = Trim(Mid(Range("A5"), 8, 2))
If shift = "0" Then
fnameext = "_AD"
Else
fnameext = ""
End If
Select Case MyName
Case "Doc: 2002"
Fname = "Document_1" & fnameext & ".txt"
Case "Doc: 6000"
Fname = "Document_2" & fnameext & ".txt"
Case "Doc: 1002"
Fname = "Document_3" & fnameext & ".txt"
Case "Doc: 4007"
Fname = "Document_4" & fnameext & ".txt"
Case "Doc: 1012"
Fname = "Document_5" & fnameext & ".txt"
Case "Doc: 2013"
Fname = "Document_6" & fnameext & ".txt"
Case "Doc: 2015"
Fname = "Document_7" & fnameext & ".txt"
Case "Doc: 4002"
Fname = "Document_8" & fnameext & ".txt"
Case "Doc: 5002"
Fname = "Document_9" & fnameext & ".txt"
Case "Doc: 4001"
Fname = "Document_10" & fnameext & ".txt"
Case "Doc: 3013"
Fname = "Document_11" & fnameext & ".txt"
Case "Doc: 6002"
Fname = "Document_12" & fnameext & ".txt"
Case "Doc: 5015"
Fname = "Document_13" & fnameext & ".txt"
Case "Doc: 2025"
Fname = "Document_14" & fnameext & ".txt"
Case "Doc: 1008"
Fname = "Document_15" & fnameext & ".txt"
Case "Doc: 2012"
Fname = "Document_16" & fnameext & ".txt"
Case "Doc: 5013"
Fname = "Document_17" & fnameext & ".txt"
Case "Doc: 2051"
Fname = "Document_18" & fnameext & ".txt"
Case "Doc: 2050"
Fname = "Document_19" & fnameext & ".txt"
Case "Doc: 2054"
Fname = "Document_20" & fnameext & ".txt"
Case "Doc: 3008"
Fname = "Document_21" & fnameext & ".txt"
Case "Doc: 7002"
Fname = "Document_22" & fnameext & ".txt"
Case "Doc: 1009"
Fname = "Document_23" & fnameext & ".txt"
Case "Doc: 4015"
Fname = "Document_24" & fnameext & ".txt"
Case "Doc: 2014"
Fname = "Document_25" & fnameext & ".txt"
Case "Doc: 3004"
Fname = "Document_26" & fnameext & ".txt"
Case "Doc: 1075"
Fname = "Document_27" & fnameext & ".txt"
Case "Doc: 1076"
Fname = "Document_28" & fnameext & ".txt"
Case "Doc: 1080"
Fname = "Document_29" & fnameext & ".txt"
Case Else
Fname = "Unknown Doc " & I & ".txt"
i = i + 1
End Select
savepath = FolderPath & "\" & Fname
ActiveWorkbook.SaveAs savepath
ActiveWorkbook.Close False
Next x
For y = 1 To count
Kill "C:\My report\Text Files\Text_" & y & ".txt"
Next y
Application.ScreenUpdating = True
End Sub
Bookmarks