hi
I have the following code to list textiles in a folder. How would i modify it to recursively search subfolders
so that i end up with a long list of filenames?
thanks
Sub ArrayDeleter()
Dim Path As String, Fso As FileSystemObject, sFile As String
Dim Var() As String, Folder As Folder, File As File, I As Long
Dim subFolder
Application.ScreenUpdating = False
wksMovies.Cells.ClearContents
Path = "C:\Temp\a\"
Set Fso = New FileSystemObject
Set Folder = Fso.GetFolder(Path)
Set subFolder = Folder.SubFolders
'Place filenames in array
For Each File In Folder.Files
If File.Name Like "*.txt" Then
I = I + 1
ReDim Preserve Var(1 To I)
Var(I) = File.Path
End If
Next File
'Dump the filenames onto a worksheet
If Len(Join(Var)) > 0 Then 'Check if any filenames in array first. If not advise user.
With wksMovies
.Activate
.Range("A1").Value = "Name"
.Range("A1").AutoFilter
.Range("A1").Font.Bold = True
.Range("A2").Resize(UBound(Var, 1), 1) = Application.Transpose(Var)
.Columns("A").AutoFit
End With
'Go down the list Deleting the files
For I = 1 To UBound(Var)
sFile = Var(I)
Fso.DeleteFile (sFile)
Next I
Else
MsgBox "No files to delete"
End If
'Cleanup
Set Fso = Nothing
Erase Var
Application.ScreenUpdating = True
End Sub
Bookmarks