Raju Kumar Singh
New Member
- Joined
- Jul 12, 2017
- Messages
- 15
Hello everyone,
I want to access all files available in a folder and its all sub folder. I have written VBA code shown below using file system object, but it is not working. Please check, what mistake is underlying.
Sub checking_files_in_multiple_folders_and_subfolders(mainpath As String)
Dim fso As Scripting.FileSystemObject
Dim fol As Scripting.Folder
Dim subfol As Scripting.Folder
Dim fil As Scripting.File
Dim mainpath As String, var1 As Byte
var1 = 1
mainpath = "D:\Excel n VBA\VBA scenarios\FSO\Recursive loop"
'**********mysht is a code name of sheet1**********
With mysht
.Range("A1").Value = "File name"
.Range("B1").Value = "File Size"
.Range("C1").Value = "Date Created"
.Range("A1").Select
End With
Set fso = New Scripting.FileSystemObject
Set fol = fso.GetFolder(mainpath)
For Each fil In fol.Files
ActiveCell.Offset(var1, 0).Value = fil.Name
ActiveCell.Offset(var1, 1).Value = fil.Size
ActiveCell.Offset(var1, 2).Value = fil.DateCreated
var1 = var1 + 1
Next fil
For Each subfol In fol.SubFolders
mypath = subfol.path
Call checking_files_in_multiple_folders_and_subfolders(subfol.path)
Next subfol
End Sub
Thanks
I want to access all files available in a folder and its all sub folder. I have written VBA code shown below using file system object, but it is not working. Please check, what mistake is underlying.
Sub checking_files_in_multiple_folders_and_subfolders(mainpath As String)
Dim fso As Scripting.FileSystemObject
Dim fol As Scripting.Folder
Dim subfol As Scripting.Folder
Dim fil As Scripting.File
Dim mainpath As String, var1 As Byte
var1 = 1
mainpath = "D:\Excel n VBA\VBA scenarios\FSO\Recursive loop"
'**********mysht is a code name of sheet1**********
With mysht
.Range("A1").Value = "File name"
.Range("B1").Value = "File Size"
.Range("C1").Value = "Date Created"
.Range("A1").Select
End With
Set fso = New Scripting.FileSystemObject
Set fol = fso.GetFolder(mainpath)
For Each fil In fol.Files
ActiveCell.Offset(var1, 0).Value = fil.Name
ActiveCell.Offset(var1, 1).Value = fil.Size
ActiveCell.Offset(var1, 2).Value = fil.DateCreated
var1 = var1 + 1
Next fil
For Each subfol In fol.SubFolders
mypath = subfol.path
Call checking_files_in_multiple_folders_and_subfolders(subfol.path)
Next subfol
End Sub
Thanks