Sub renameFiles() Dim main_path As String Dim fileName As String Dim newFileName As String Dim pos As Long Dim posDot As Long Dim numPart As String Dim ext As String ' 取得資料夾路徑 main_path = Sheets("Main").Range("B2").Value If Right(main_path, 1) <> "\" Then main_path = main_path & "\" End If fileName = Dir(main_path & "*.*") Do While fileName <> "" pos = InStrRev(fileName, "_") ' 找到最後一個 "_" posDot = InStrRev(fileName, ".") ' 找到最後一個 "." If pos > 0 And posDot > pos Then
numPart = mid(fileName, pos + 1, posDot - pos - 1) If IsNumeric(numPart) Then
Name main_path & fileName As main_path & newFileName Debug.Print "Rename: " & fileName & " -> " & newFileName End If End If fileName = Dir Loop MsgBox "重新編號完成!", vbInformation End Sub