ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Listing the contents of a folder (https://www.excelbanter.com/excel-discussion-misc-queries/138633-listing-contents-folder.html)

[email protected]

Listing the contents of a folder
 
I am looking to write a macro that will list the contents of a
specified folder, including the contents of subfolders. Is this
something that i can do with a macro?

Thanks.

Asa


Barb Reinhardt

Listing the contents of a folder
 
It would help if you mapped the folder that you want to list out to a drive.

I do this as follows:

Start - Run - cmd

I have multiple drives mapped. Let's say I want to get a listing of the Y
drive.

I type in Y:

I then type:

dir/s z:textfile.txt

It will save a listing of the Y drive on my Z drive as a file named
textfile.txt

Good luck.


" wrote:

I am looking to write a macro that will list the contents of a
specified folder, including the contents of subfolders. Is this
something that i can do with a macro?

Thanks.

Asa



Jim Cone

Listing the contents of a folder
 

Change strPath to the desired folder.
Listing added to Column B of active sheet.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware

Sub ListAllFilesInFolder()
'Jim Cone - San Francisco - Oct 2006
Dim strPath As String
Dim oFSO As Object
Dim oFile As Object
Dim oFolder As Object

Dim N As Long
N = 1
strPath = "C:\Documents and Settings\Name\My Documents\Excel Files"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(strPath)
Cells(N, 1).Value = oFolder.Path
N = N + 1
For Each oFile In oFolder.Files
Cells(N, 2).Value = oFile.Name
N = N + 1
Next 'oFile
Call ListSubFolderFiles(oFolder, N)

Set oFSO = Nothing
Set oFile = Nothing
Set oFolder = Nothing
End Sub

Function ListSubFolderFiles(ByRef oParentFolder As Object, ByRef lngR As Long)
Dim oSubFolder As Object
Dim oFile As Object
For Each oSubFolder In oParentFolder.SubFolders
Cells(lngR, 1).Value = oSubFolder.Path
Range(Cells(lngR, 1), Cells(lngR, 5)).Interior.ColorIndex = 15
lngR = lngR + 1
For Each oFile In oSubFolder.Files
Cells(lngR, 2).Value = oFile.Name
lngR = lngR + 1
Next
ListSubFolderFiles oSubFolder, lngR
Next 'oSubFolder
End Function
'---------------



wrote in message
I am looking to write a macro that will list the contents of a
specified folder, including the contents of subfolders. Is this
something that i can do with a macro?
Thanks.
Asa



All times are GMT +1. The time now is 05:13 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com