Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Change file names in a folder

Try this code...


Function GetAllFilesInDir(ByVal strDirPath As String) As Variant
' Loop through the directory specified in strDirPath and save each
' file name in an array, then return that array to the calling
' procedure.
' Return False if strDirPath is not a valid directory.
Dim strTempName As String
Dim varFiles() As Variant
Dim lngFileCount As Long

On Error GoTo GetAllFiles_Err

' Make sure that strDirPath ends with a "\" character.
If Right$(strDirPath, 1) < "\" Then
strDirPath = strDirPath & "\"
End If

' Make sure strDirPath is a directory.
If GetAttr(strDirPath) = vbDirectory Then
strTempName = Dir(strDirPath, vbDirectory)
Do Until Len(strTempName) = 0
' Exclude ".", "..".
If (strTempName < ".") And (strTempName < "..") Then
' Make sure we do not have a sub-directory name.
If (GetAttr(strDirPath & strTempName) _
And vbDirectory) < vbDirectory Then
' Increase the size of the array
' to accommodate the found filename
' and add the filename to the array.
ReDim Preserve varFiles(lngFileCount)
varFiles(lngFileCount) = strTempName
lngFileCount = lngFileCount + 1
End If
End If
' Use the Dir function to find the next filename.
strTempName = Dir()
Loop
' Return the array of found files.
GetAllFilesInDir = varFiles
End If
GetAllFiles_End:
Exit Function
GetAllFiles_Err:
GetAllFilesInDir = False
Resume GetAllFiles_End
End Function

Sub RenameFiles()
Dim varFiles As Variant
Dim i As Integer
Dim strFileNameOld, strFileNameNew, strDate
Dim strDir As String

'change this to your dir
strDir = "C:\Mis documentos\12 December 2005\"

'Calls the above function
varFiles = GetAllFilesInDir(strDir)

' from zero to the size of hte array (which now contains all files in
the directory)
For i = 0 To UBound(varFiles)
' date
strDate = Format(DateSerial(Year(Date), Month(Date) - 1, 1),
"yyyy-mm")
strFileNameOld = strDir & varFiles(i)
' new file name with the file suffix (e.g. .xls) reappeneded
strFileNameNew = strDir & Left(varFiles(i), Len(varFiles(i)) - 4) &
strDate & Right(varFiles(i), 4)
Name strFileNameOld As strFileNameNew
Next i

End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a list of file names within a folder in Microsoft Ex Seablue Excel Discussion (Misc queries) 2 April 9th 07 08:44 PM
Change names of Files in a folder TISR Excel Programming 1 April 1st 06 10:40 AM
Compare file names to folder contents marlea[_14_] Excel Programming 3 March 16th 06 03:59 PM
Change names of files in a folder to match names in Excel Column saybut Excel Programming 4 February 9th 04 06:26 PM
Extract file names last save info from a Folder Harapa Excel Programming 0 November 16th 03 04:33 PM


All times are GMT +1. The time now is 02:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"