Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default List files from a direcotry

Hi

I am trying to list the file path and all files in a directory on an Excel
spreadsheet for an inventory ready for Freedom of Information legislation.
With the help of a friend I have come up with the following code;

Sub folders()

'Worksheets.Add
Sheets.Add after:=Worksheets(1)
ActiveSheet.Name = "Folders " & Format(Now, "dd-mmm-yyyy hh-mm-ss AM/PM")

' Set column headings
With Range("A1")
.FormulaR1C1 = "File Path"
End With

' Set column widths
Range("A:A").ColumnWidth = 65

' Parameter to create listing
ListFolders "C:\", True
'Columns(1).AutoFit


End Sub

Sub ListFolders(Src As String, IncSub As Boolean)
Dim FSO As Object
Dim F As Object
Dim SubF As Object
Dim r As Long

On Error Resume Next

Set FSO = CreateObject("Scripting.FileSystemObject")
Set F = FSO.GetFolder(Src)
r = Cells(65536, 1).End(xlUp).Row + 1
Cells(r, 1).Value = F.path
If IncSub Then
For Each SubF In F.SubFolders
ListFolders SubF.path, True
Next SubF
End If

What it does not do is give me the individual file names and ideally I would
like the individual directories and sub directories in seperate columns. Can
anyone give me a pointer?

Thanks
SteveB


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default List files from a direcotry

Steveb,

My Excel add-in "List Files" does that.
It searches for the file type you specify or for all files.
Files and folders in the specified directory are listed.
Sub-folders also listed if specified by user
File name, size, date, type are provided
Directories/folder path is listed in one column and files in the adjacent column.
Each file is hyperlinked.
Available - free - upon direct request.
Remove xxx from my email address

Regards,
Jim Cone
San Francisco, CA
XX

"Steveb" wrote in message ...
Hi
I am trying to list the file path and all files in a directory on an Excel
spreadsheet for an inventory ready for Freedom of Information legislation.
With the help of a friend I have come up with the following code;
Sub folders()
'Worksheets.Add
Sheets.Add after:=Worksheets(1)
ActiveSheet.Name = "Folders " & Format(Now, "dd-mmm-yyyy hh-mm-ss AM/PM")
' Set column headings
With Range("A1")
.FormulaR1C1 = "File Path"
End With
' Set column widths
Range("A:A").ColumnWidth = 65

' Parameter to create listing
ListFolders "C:\", True
'Columns(1).AutoFit
End Sub
Sub ListFolders(Src As String, IncSub As Boolean)
Dim FSO As Object
Dim F As Object
Dim SubF As Object
Dim r As Long
On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
Set F = FSO.GetFolder(Src)
r = Cells(65536, 1).End(xlUp).Row + 1
Cells(r, 1).Value = F.path
If IncSub Then
For Each SubF In F.SubFolders
ListFolders SubF.path, True
Next SubF
End If
What it does not do is give me the individual file names and ideally I would
like the individual directories and sub directories in seperate columns. Can
anyone give me a pointer?
Thanks
SteveB


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default List files from a direcotry

You might want to try this macro. It opens a file browser and copies the
selected file names to the active cell.

Dim vrtNames As Variant
Dim vrtNames2() As Variant
Dim vrtParsedName As Variant
Dim i As Long

vrtNames = Application.GetOpenFilename(, , , , True)
If VarType(vrtNames) And vbArray Then
ReDim vrtNames2(1 To UBound(vrtNames), 1 To 1)
For i = 1 To UBound(vrtNames)
vrtParsedName = Split(Expression:=vrtNames(i), Delimiter:="\")
vrtNames2(i, 1) = vrtParsedName(UBound(vrtParsedName))
Next i
ActiveCell.Resize(UBound(vrtNames2, 1), 1) = vrtNames2
End If




"Steveb" wrote:

Hi

I am trying to list the file path and all files in a directory on an Excel
spreadsheet for an inventory ready for Freedom of Information legislation.
With the help of a friend I have come up with the following code;

Sub folders()

'Worksheets.Add
Sheets.Add after:=Worksheets(1)
ActiveSheet.Name = "Folders " & Format(Now, "dd-mmm-yyyy hh-mm-ss AM/PM")

' Set column headings
With Range("A1")
.FormulaR1C1 = "File Path"
End With

' Set column widths
Range("A:A").ColumnWidth = 65

' Parameter to create listing
ListFolders "C:\", True
'Columns(1).AutoFit


End Sub

Sub ListFolders(Src As String, IncSub As Boolean)
Dim FSO As Object
Dim F As Object
Dim SubF As Object
Dim r As Long

On Error Resume Next

Set FSO = CreateObject("Scripting.FileSystemObject")
Set F = FSO.GetFolder(Src)
r = Cells(65536, 1).End(xlUp).Row + 1
Cells(r, 1).Value = F.path
If IncSub Then
For Each SubF In F.SubFolders
ListFolders SubF.path, True
Next SubF
End If

What it does not do is give me the individual file names and ideally I would
like the individual directories and sub directories in seperate columns. Can
anyone give me a pointer?

Thanks
SteveB



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
getting list of files Esradekan Excel Worksheet Functions 3 July 25th 08 10:20 AM
list all the files on my computer Dave F Excel Discussion (Misc queries) 2 December 10th 06 02:50 PM
List Files in excel PeterO Excel Discussion (Misc queries) 2 June 15th 05 01:04 AM
list box- list all files ina directory suee Excel Programming 9 April 7th 04 02:32 AM
adding Tiff files to the list of image files ksel[_2_] Excel Programming 0 January 23rd 04 09:22 PM


All times are GMT +1. The time now is 01:36 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"