Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Pablo
 
Posts: n/a
Default Listing Directory Contents in Worksheet

Is there a way to list the names of files within a directory in a worksheet?
I have a directory that contains 800 files and I would like to list them
within a Excel worksheet. I have tried selecting all, copy and paste but
nothing happens.

Any help is greatly appreciated.

Thanks,
Paul
  #2   Report Post  
Bob Phillips
 
Posts: n/a
Default

Paul,

One way

Option Explicit

'-------------------------------------------------------------------------
Public Sub Hyperlinked_Folder_List()
'-------------------------------------------------------------------------
Const HFL_FOLDER As String = "C:\myTest"
Dim oFSO As Object
Dim cList As Long
Dim aryfiles
Dim iLevel As Long
Dim i As Long
Dim sFolder As String
Dim oSheet As Worksheet

Set oFSO = CreateObject("Scripting.FileSystemObject")

aryfiles = Array()
cList = -1: iLevel = 1

ReDim aryfiles(1, 0)
SelectFiles aryfiles, oFSO, cList, iLevel, HFL_FOLDER
On Error Resume Next
Set oSheet = Worksheets("Files")
On Error GoTo 0
If Not oSheet Is Nothing Then
oSheet.Cells.ClearContents
Else
Worksheets.Add.Name = "Files"
End If

With ActiveSheet
For i = LBound(aryfiles, 2) To UBound(aryfiles, 2)
.Hyperlinks.Add Anchor:=.Cells(i + 1, aryfiles(1, i)), _
Address:=aryfiles(0, i), _
TextToDisplay:=aryfiles(0, i)
Next
.Columns("A:Z").EntireColumn.AutoFit
End With

Set oFSO = Nothing

End Sub


'-----------------------------**-----------------------------*-*------------
Private Sub SelectFiles(ByRef aryfiles, _
ByVal FSO As Object, _
ByRef pzList As Long, _
ByRef pzLevel As Long, _
ByVal pzPath As String)
'-----------------------------**-----------------------------*-*------------
Dim oSubfolder As Object
Dim oFolder As Object
Dim oFile As Object
Dim oFiles As Object

Set oFolder = FSO.GetFolder(pzPath)

Set oFiles = oFolder.Files
For Each oFile In oFiles
pzList = pzList + 1
ReDim Preserve aryfiles(1, pzList)
aryfiles(0, pzList) = oFolder.Path & "\" & oFile.Name
aryfiles(1, pzList) = pzLevel
Next oFile

pzLevel = pzLevel + 1
For Each oSubfolder In oFolder.Subfolders
SelectFiles aryfiles, FSO, pzList, pzLevel, oSubfolder.Path
Next oSubfolder
pzLevel = pzLevel - 1

Set oFiles = Nothing
Set oFolder = Nothing

End Sub
'-------------------------------------------------------------------------





--

HTH

RP
(remove nothere from the email address if mailing direct)


"Pablo" wrote in message
...
Is there a way to list the names of files within a directory in a

worksheet?
I have a directory that contains 800 files and I would like to list them
within a Excel worksheet. I have tried selecting all, copy and paste but
nothing happens.

Any help is greatly appreciated.

Thanks,
Paul



  #3   Report Post  
Bernie Deitrick
 
Posts: n/a
Default

Paul,

You could run a macro, like the one below. This will prompt you to select a file within the folder
of interest, and then list the file, date, and file size for all files found in the folder.

HTH,
Bernie
MS Excel MVP

Sub ListFolderContents()
Dim i As Integer
Dim myName As String
With Application.FileSearch
.NewSearch
myName = Application.GetOpenFilename( _
Title:="Pick a file in your folder")
.LookIn = Left(myName, InStrRev(myName, "\"))
.SearchSubFolders = False
.MatchTextExactly = False
.FileType = msoFileTypeAllFiles
If .Execute() 0 Then
MsgBox "There were " & .FoundFiles.Count & " file(s) found."
For i = 1 To .FoundFiles.Count
Cells(i, 1).Value = .FoundFiles(i)
Cells(i, 2).Value = FileDateTime(.FoundFiles(i))
Cells(i, 3).Value = FileLen(.FoundFiles(i))
Next i
Range("A:C").EntireColumn.AutoFit
Else
MsgBox "There were no files found."
End If
End With
End Sub



"Pablo" wrote in message
...
Is there a way to list the names of files within a directory in a worksheet?
I have a directory that contains 800 files and I would like to list them
within a Excel worksheet. I have tried selecting all, copy and paste but
nothing happens.

Any help is greatly appreciated.

Thanks,
Paul



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy from worksheet to another x times Union70 Excel Discussion (Misc queries) 0 March 7th 05 09:03 PM
Worksheet Function - Find? DAA Excel Worksheet Functions 2 February 24th 05 04:15 PM
grand total column B from every worksheet in workbook igor Excel Discussion (Misc queries) 2 February 23rd 05 08:42 PM
Linking items GREATER THAN O on another worksheet in the same Work Eddie Shapiro Excel Discussion (Misc queries) 4 December 1st 04 02:55 PM
Worksheet name and Backward compatibility Rich Excel Discussion (Misc queries) 3 November 30th 04 06:10 PM


All times are GMT +1. The time now is 07:06 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"