'Get all file name in folder using
'==================================
Option Explicit
Dim i As Long
Dim Arr(1 To 10000, 1 To 3)
Function Getfile(Linkfolder As String)
Dim sFolder As Object 'SubFolder
Dim sfi As Object 'Subfolder of SubFolder
Dim fi As Object 'File
With CreateObject("Scripting.filesystemobject")
Set sFolder = .GetFolder(Linkfolder).SubFolders
If sFolder.Count > 0 Then
'If have files on Linkfolder then Get File Name
If .GetFolder(Linkfolder).Files.Count > 0 Then
For Each fi In .GetFolder(Linkfolder).Files
If Left(fi.Name, 1) <> "~" Then
i = i + 1
Arr(i, 1) = i
Arr(i, 3) = fi.Name
Arr(i, 2) = Linkfolder
End If
Next
End If
'De quy
For Each sfi In sFolder
Getfile (sfi)
Next
Else
'Get FileName on the Sub Folder
For Each fi In .GetFolder(Linkfolder).Files
'Exclude Temporary file
If Left(fi.Name, 1) <> "~" Then
i = i + 1
Arr(i, 1) = i
Arr(i, 3) = fi.Name
Arr(i, 2) = Linkfolder
End If
Next
End If
End With
End Function
Sub GetFilename()
Dim source As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
.AllowMultiSelect = False
source = .SelectedItems(1)
End With
i = 0
Getfile (source)
Sheet1.Range("A2:D65536").ClearContents
Sheet1.Range("A2").Resize(i, 3) = Arr
End Sub
Sub Rename()
On Error Resume Next
Dim source As String, RenameArr
Dim t As Long
RenameArr = Sheet1.Range("A2:D" & Sheet1.Range("B65536").End(3).Row)
With CreateObject("Scripting.filesystemobject")
For t = 1 To UBound(RenameArr)
.MoveFile RenameArr(t, 2) & "\" & RenameArr(t, 3), RenameArr(t, 2) & "\" & RenameArr(t, 4)
Next
End With
MsgBox "Finish", vbInformation
End Sub