Is it possible to list both files and folders in a directory?
Using the "FileSystemObject" method.
Is it possible to list both files and folders in a directory?
Using the "FileSystemObject" method.
Something like this perhaps...
![]()
Option Explicit Sub RenameFolder() Dim Path As String Dim Fso As Object, Folder As Object, Subfolder As Object, SubFld As Object, File As Object Path = ThisWorkbook.Path Set Fso = CreateObject("Scripting.FileSystemObject") Set Folder = Fso.GetFolder(Path) For Each Subfolder In Folder.subfolders Debug.Print Subfolder For Each SubFld In Subfolder.subfolders Debug.Print SubFld For Each File In SubFld.Files Debug.Print File Next File Next SubFld Next Subfolder End Sub
Good Luck...
I don't presume to know what I am doing, however, just like you, I too started somewhere...
One-day, One-problem at a time!!!
If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
Also....Add a comment if you like!!!!
And remember...Mark Thread as Solved...
Excel Forum Rocks!!!
@sintek, your code gives only folder or file. I needed both.
For example, list in snapshot should be:
New folder1
New folder2
New Microsoft Excel Worksheet1
New Microsoft Word Document1
Capture.JPG
So...You do not want the files within the subfolders of the folders...
You just want first level Folders and files...
Yes I do want subfolders but that will come later.
The hard part seems to be listing folders together with files.
Perhaps I just don't understand...
If for example on your desktop you have 2 Folders and two documents...
Must code show those 4 only....Not go in further
Yes, show 2 folders and 2 documents.
Yes, go to subfolders where they exist.
Harder than I thought.
![]()
Option Explicit Sub listfile() Dim path As String Dim vaArray(), i As Long Dim oFile As Object, oFSO As Object, oFolder As Object, oFiles As Object path = ThisWorkbook.path Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(path & "\") Set oFiles = oFolder.Files If oFiles.Count = 0 Then Exit Sub ReDim vaArray(1 To oFiles.Count) i = 1 For Each oFile In oFiles vaArray(i) = oFile.Name i = i + 1 Next oFile Range("A1").Resize(i) = Application.Transpose(vaArray) End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks