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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

renaming files

Status
Not open for further replies.

Machiaveli

Programmer
Dec 16, 2003
91
NL
Hi,

I have some files in different directies which i want to rename because they have the same names. The new name has a date then a "_" and then it's original name. E.g. 20041119_test1.txt.
The root folder is "c:\results\" and the subdirectories are like "20041119-123456".

Can someone help me with modifying the following code, it doesn't rename all files in the directories. I can only manage to point one folder and then rename them.

Public Sub NameFilesWithFolder(cPath As String)
'you must pass a valid path to this routine; it may end in a backslash
Dim cFile As String
Dim cPrefix As String
Dim nPos As Long
'find the last folder in cPath
If Right(cPath, 1) <> "\" Then cPath = cPath & "\"
nPos = Len(cPath) - 1
Do Until Mid(cPath, nPos, 1) = "\"
nPos = nPos - 1
If nPos = 1 Then Exit Do 'just in case there is no \ at all
Loop
cPrefix = Mid(cPath, nPos + 1, 8) & "_" 'taking the first 8 characters of this folder
'must be the date
cFile = Dir(cPath & "*.*")
Do Until cFile = ""
Name cPath & cFile As cPath & cPrefix & cFile
cFile = Dir() 'will return "" when no more files
Loop
End Sub

 
Hi p27br,

Thanks for the tip, but it's a little bit to much to understand to get it worked.

Can you help me with my code?
 
Your problem is that while you browse the folder with the Dir function you make change in it with the Name instruction.
You may consider to use a dynamic array (or collection or dictionary) to grab all the files you want to rename and then browse this static list for the rename stuff.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top