Machiaveli
Programmer
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
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