Home |
Search |
Today's Posts |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
The Filesearch method is less code, but it is slower and it relies on a
reference to the Filesearch library, so I prefer my method. To avoid an error and make the added number one higher I made a small adaptation: Sub RenameFiles(strFolder As String, _ strExtension As String, _ strFileExclude As String, _ strNewName As String, _ lMaxFiles As Long, _ Optional bKillOld As Boolean = False) Dim arr Dim i As Long Dim lCounter As Long Dim lCounterAdd As Long On Error GoTo ERROROUT arr = GetFilesInFolder(strFolder & "\*." & strExtension) For i = 1 To UBound(arr) If arr(i) < strFileExclude And _ lCounter < lMaxFiles Then Do While Len(Dir(strFolder & "\" & _ strNewName & lCounter + 1 + lCounterAdd & _ "." & strExtension)) 0 lCounterAdd = lCounterAdd + 1 Loop FileCopy strFolder & "\" & arr(i), _ strFolder & "\" & strNewName & lCounter + 1 + lCounterAdd & "." & strExtension lCounter = lCounter + 1 If bKillOld Then Kill strFolder & "\" & arr(i) End If End If Next Exit Sub ERROROUT: MsgBox "no files found", , "rename files in folder" On Error GoTo 0 End Sub RBS "neowok" wrote in message ... thanks, I have found a shorter solution which is Sub renfiles() Dim I As Long Dim NoFiles As Long Dim strOldName As String Dim strNewName As String With Application.FileSearch NewSearch LookIn = ThisWorkbook.Path FileType = msoFileTypeExcelWorkbooks Execute NoFiles = IIf(.FoundFiles.Count 40, 40, .FoundFiles.Count - 1) For I = 0 To NoFiles If .FoundFiles(I + 1) < ThisWorkbook.FullName Then strOldName = .FoundFiles(I + 1) strNewName = ThisWorkbook.Path & "\EX" & Format(I + 1, "0") & ".xls" Name strOldName As strNewName End If Next I End With End Sub the only problem I have with this one at the moment is if an ex1 etc file already exists when it tries to rename a file to ex1 then it causes a runtime error, when it should rename it to ex2 instead if ex1 already exists. thanks -- neowok ------------------------------------------------------------------------ neowok's Profile: http://www.excelforum.com/member.php...fo&userid=5940 View this thread: http://www.excelforum.com/showthread...hreadid=394886 |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Renaming excel files in a folder | Excel Discussion (Misc queries) | |||
Excel Renaming Files When Opening Using File Convertor | Excel Discussion (Misc queries) | |||
How to print a bunch of files form the list (excel)? | Excel Discussion (Misc queries) | |||
Renaming Files w/Excel | Excel Discussion (Misc queries) | |||
Batch renaming of many worksheets in Excel files? | Excel Discussion (Misc queries) |