Friday, February 17, 2012

Save the file names of folder in a dynamic array

If you want to store the file names in a dynamic array.

Here is the code-

Option Base 1
Sub filenamesinfolder()
Dim fld, fil As Object
Dim abc() As String, i As Long, j As Long
i = 1
Set fso = CreateObject("scripting.filesystemobject")
' folder path change it here.
Set fld = fso.getfolder("C:\Documents and Settings\user\Desktop\")
' save file names in a array
For Each fil In fld.Files

' redim and presever will increase the size of array without loosing the previous data stored in a array
ReDim Preserve abc(i)
abc(i) = fil.Name
i = i + 1
Next fil
' print the data of array in a worksheet
For j = LBound(abc) To UBound(abc)
Cells(j, 1) = abc(j)
End Sub

1 comment: