MichaelFa
07-27-2018, 07:50 AM
I have 390 folders with the same file name contained in each one (index.txt). The following code will rename all of the files in the folders with the same name. I need to insert a unique variable as part of the file name**. The goal is to concantonate the files and import into Excel. I "borrowed" the code and have no experience with coding or variables. :(
**newName = "\myNewFileName" & ".txt" -- Line 19 on the code below
<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Sub RenAllFilesInclSubFold()
Dim fso As Object, fold As Object, fFile As Object
Dim fPath As String, fName As String, newName As String
fPath = "C:\temp\OnBaseBackup" ' ----- initial folder that contains all the subfolders
cnt = ""
'ren files in subfolders
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder(fPath)
For Each fFile In fold.subfolders
'the files i want to rename and move, start with "inde"
fName = Dir(fFile.Path & "\inde*", vbNormal)
Do While fName <> ""
newName = "\myNewFileName" & ".txt"
Name fFile.Path & "" & fName As fFile.Path & newName
fso.MoveFile Source:=fFile.Path & newName, Destination:="C:\temp\OnBaseResults" 'this code will not create the new folder
fName = Dir
Loop
Next
End Sub
**newName = "\myNewFileName" & ".txt" -- Line 19 on the code below
<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Sub RenAllFilesInclSubFold()
Dim fso As Object, fold As Object, fFile As Object
Dim fPath As String, fName As String, newName As String
fPath = "C:\temp\OnBaseBackup" ' ----- initial folder that contains all the subfolders
cnt = ""
'ren files in subfolders
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder(fPath)
For Each fFile In fold.subfolders
'the files i want to rename and move, start with "inde"
fName = Dir(fFile.Path & "\inde*", vbNormal)
Do While fName <> ""
newName = "\myNewFileName" & ".txt"
Name fFile.Path & "" & fName As fFile.Path & newName
fso.MoveFile Source:=fFile.Path & newName, Destination:="C:\temp\OnBaseResults" 'this code will not create the new folder
fName = Dir
Loop
Next
End Sub