Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enumerating and renaming files

Status
Not open for further replies.

kpereira

Programmer
Jun 29, 2000
33
0
0
US
Can anyone tell me how to enumerate through all of the files in a directory and rename them. For example I have a directory with files such as 1.bmp, 10.bmp, 200.bmp. I want to rename the file respectively, DMS1.bmp, DMS10.bmp, DMS200.bmp.

I haven't worked much with file objects. Any help would be greatly appreciated.
 
This is how I iterate through directories to do file manipulation. I edited this code to rename files and it works like a champ.


Dim oFSO As Scripting.FileSystemObject

Private Sub Form_Load()
Set oFSO = New Scripting.FileSystemObject
iterateAndChange App.Path & "\files"
End Sub

Private Function iterateAndChange(ByVal sDir As String) As String
Dim oDir As Scripting.Folder
Dim oSDir As Scripting.Folder
Dim oFile As Scripting.File
Dim i As Integer
Dim k As Integer

Set oDir = oFSO.GetFolder(sDir)
For Each oFile In oDir.Files
Debug.Print oFile.Path
oFile.Name = "_" & oFile.Name
Next
For Each oSDir In oDir.SubFolders
iterateAndChange oSDir.Path
Next
End Function


Enjoy !!
MattU
 

Sorry if I didn't explain my code earlier. I was in a little bit of a hurry.



Dim oFSO As Scripting.FileSystemObject

Private Sub Form_Load()
Set oFSO = New Scripting.FileSystemObject
iterateAndChange App.Path & "\files"
End Sub


'This is a recursive function that first
'applies changes to files in the current
'directory, gets the next directory in
'the current directory calls the function
'again(recursivly) applies changes to it's
'files, repeat ...

Private Function iterateAndChange(ByVal sDir As String) As String
Dim oDir As Scripting.Folder
Dim oSDir As Scripting.Folder
Dim oFile As Scripting.File

'Not Needed, I guess I am a sloppy coder
Dim i As Integer
Dim k As Integer

Set oDir = oFSO.GetFolder(sDir)

For Each oFile In oDir.Files
' Start : -Make your changes here-
Debug.Print oFile.Path
oFile.Name = "_" & oFile.Name
' End : -Make your changes here-
Next

For Each oSDir In oDir.SubFolders
iterateAndChange oSDir.Path
Next

End Function



Sorry I did explain earlier

Matt


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top