Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default List Filenames and Date Modified

Can someone help me create a macro that lists excel files in a folder
and also gets the date the file was created and last modified?

I found this macro on the web, but it just gets the filenames:

Public Sub ListWorkbooks()
Dim Directory As String
Dim FileName As String
Dim IndexSheet As Worksheet
Dim rw As Long

'Change the directory below as needed
Directory = "R:\07\"
If Left(Directory, 1) < "\" Then
Directory = Directory & "\"
End If

rw = 1

Set IndexSheet = ThisWorkbook.ActiveSheet

FileName = Dir(Directory & "*.xls")
Do While FileName < ""
IndexSheet.Cells(rw, 1).Value = FileName
rw = rw + 1
FileName = Dir
Loop

Set IndexSheet = Nothing
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,420
Default List Filenames and Date Modified

Option Explicit

Private cnt As Long
Private arfiles
Private level As Long

Sub Folders()
Dim i As Long
Dim sFolder As String
Dim iStart As Long
Dim iEnd As Long
Dim fOutline As Boolean

arfiles = Array()
cnt = -1
level = 1

sFolder = "C:\test\"
ReDim arfiles(4, 0)
If sFolder < "" Then
SelectFiles sFolder
Application.DisplayAlerts = False
On Error Resume Next
Worksheets("Files").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Worksheets.Add.Name = "Files"
With ActiveSheet
For i = LBound(arfiles, 2) To UBound(arfiles, 2)
If arfiles(0, i) = "" Then
If fOutline Then
Rows(iStart + 1 & ":" & iEnd).Rows.Group
End If
With .Cells(i + 1, arfiles(4, i))
.Value = arfiles(1, i)
.Font.Bold = True
End With
iStart = i + 1
iEnd = iStart
fOutline = False
Else
.Hyperlinks.Add Anchor:=.Cells(i + 1, arfiles(4, i)), _
Address:=arfiles(0, i), _
TextToDisplay:=arfiles(1, i)
.Cells(i + 1, "C").Value = arfiles(2, i)
.Cells(i + 1, "D").Value = arfiles(3, i)
iEnd = iEnd + 1
fOutline = True
End If
Next
.Columns("A:Z").AutoFit
End With
End If
'just in case there is another set to group
If fOutline Then
Rows(iStart + 1 & ":" & iEnd).Rows.Group
End If

ActiveSheet.Outline.ShowLevels RowLevels:=1
ActiveWindow.DisplayGridlines = False

End Sub

'-----------------------------------------------------------------------
Sub SelectFiles(Optional sPath As String)
'-----------------------------------------------------------------------
Static FSO As Object
Dim oSubFolder As Object
Dim oFolder As Object
Dim oFile As Object
Dim oFiles As Object
Dim arPath

If FSO Is Nothing Then
Set FSO = CreateObject("Scripting.FileSystemObject")
End If

If sPath = "" Then
sPath = CurDir
End If

arPath = Split(sPath, "\")
cnt = cnt + 1
ReDim Preserve arfiles(4, cnt)
arfiles(0, cnt) = ""
arfiles(1, cnt) = arPath(level - 1)
arfiles(4, cnt) = level

Set oFolder = FSO.GetFolder(sPath)
Set oFiles = oFolder.Files
For Each oFile In oFiles
cnt = cnt + 1
ReDim Preserve arfiles(4, cnt)
arfiles(0, cnt) = oFolder.Path & "\" & oFile.Name
arfiles(1, cnt) = oFile.Name
arfiles(2, cnt) = oFile.DateCreated
arfiles(3, cnt) = oFile.DateLastModified
arfiles(4, cnt) = level + 1
Next oFile

level = level + 1
For Each oSubFolder In oFolder.Subfolders
SelectFiles oSubFolder.Path
Next
level = level - 1

End Sub


--
__________________________________
HTH

Bob

"ac1179" wrote in message
...
Can someone help me create a macro that lists excel files in a folder
and also gets the date the file was created and last modified?

I found this macro on the web, but it just gets the filenames:

Public Sub ListWorkbooks()
Dim Directory As String
Dim FileName As String
Dim IndexSheet As Worksheet
Dim rw As Long

'Change the directory below as needed
Directory = "R:\07\"
If Left(Directory, 1) < "\" Then
Directory = Directory & "\"
End If

rw = 1

Set IndexSheet = ThisWorkbook.ActiveSheet

FileName = Dir(Directory & "*.xls")
Do While FileName < ""
IndexSheet.Cells(rw, 1).Value = FileName
rw = rw + 1
FileName = Dir
Loop

Set IndexSheet = Nothing
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default List Filenames and Date Modified

On Sep 30, 12:59*pm, "Bob Phillips" wrote:
Option Explicit

Private cnt As Long
Private arfiles
Private level As Long

Sub Folders()
Dim i As Long
Dim sFolder As String
Dim iStart As Long
Dim iEnd As Long
Dim fOutline As Boolean

* * arfiles = Array()
* * cnt = -1
* * level = 1

* * sFolder = "C:\test\"
* * ReDim arfiles(4, 0)
* * If sFolder < "" Then
* * * * SelectFiles sFolder
* * * * Application.DisplayAlerts = False
* * * * On Error Resume Next
* * * * Worksheets("Files").Delete
* * * * On Error GoTo 0
* * * * Application.DisplayAlerts = True
* * * * Worksheets.Add.Name = "Files"
* * * * With ActiveSheet
* * * * * * For i = LBound(arfiles, 2) To UBound(arfiles, 2)
* * * * * * * * If arfiles(0, i) = "" Then
* * * * * * * * * * If fOutline Then
* * * * * * * * * * * * Rows(iStart + 1 & ":" & iEnd).Rows.Group
* * * * * * * * * * End If
* * * * * * * * * * With .Cells(i + 1, arfiles(4, i))
* * * * * * * * * * * * .Value = arfiles(1, i)
* * * * * * * * * * * * .Font.Bold = True
* * * * * * * * * * End With
* * * * * * * * * * iStart = i + 1
* * * * * * * * * * iEnd = iStart
* * * * * * * * * * fOutline = False
* * * * * * * * Else
* * * * * * * * * * .Hyperlinks.Add Anchor:=.Cells(i + 1, arfiles(4, i)), _
* * * * * * * * * * * * * * * * * * Address:=arfiles(0, i), _
* * * * * * * * * * * * * * * * * * TextToDisplay:=arfiles(1, i)
* * * * * * * * * * .Cells(i + 1, "C").Value = arfiles(2, i)
* * * * * * * * * * .Cells(i + 1, "D").Value = arfiles(3, i)
* * * * * * * * * * iEnd = iEnd + 1
* * * * * * * * * * fOutline = True
* * * * * * * * End If
* * * * * * Next
* * * * * * .Columns("A:Z").AutoFit
* * * * End With
* * End If
* * 'just in case there is another set to group
* * If fOutline Then
* * * * Rows(iStart + 1 & ":" & iEnd).Rows.Group
* * End If

* * ActiveSheet.Outline.ShowLevels RowLevels:=1
* * ActiveWindow.DisplayGridlines = False

End Sub

'-----------------------------------------------------------------------
Sub SelectFiles(Optional sPath As String)
'-----------------------------------------------------------------------
Static FSO As Object
Dim oSubFolder As Object
Dim oFolder As Object
Dim oFile As Object
Dim oFiles As Object
Dim arPath

* * If FSO Is Nothing Then
* * * * Set FSO = CreateObject("Scripting.FileSystemObject")
* * End If

* * If sPath = "" Then
* * * * sPath = CurDir
* * End If

* * arPath = Split(sPath, "\")
* * cnt = cnt + 1
* * ReDim Preserve arfiles(4, cnt)
* * arfiles(0, cnt) = ""
* * arfiles(1, cnt) = arPath(level - 1)
* * arfiles(4, cnt) = level

* * Set oFolder = FSO.GetFolder(sPath)
* * Set oFiles = oFolder.Files
* * For Each oFile In oFiles
* * * * cnt = cnt + 1
* * * * ReDim Preserve arfiles(4, cnt)
* * * * arfiles(0, cnt) = oFolder.Path & "\" & oFile.Name
* * * * arfiles(1, cnt) = oFile.Name
* * * * arfiles(2, cnt) = oFile.DateCreated
* * * * arfiles(3, cnt) = oFile.DateLastModified
* * * * arfiles(4, cnt) = level + 1
* * Next oFile

* * level = level + 1
* * For Each oSubFolder In oFolder.Subfolders
* * * * SelectFiles oSubFolder.Path
* * Next
* * level = level - 1

End Sub

--
__________________________________
HTH

Bob

"ac1179" wrote in message

...



Can someone help me create a macro that lists excel files in a folder
and also gets the date the file was created and last modified?


I found this macro on the web, but it just gets the filenames:


Public Sub ListWorkbooks()
* Dim Directory As String
* Dim FileName As String
* Dim IndexSheet As Worksheet
* Dim rw As Long


* 'Change the directory below as needed
* Directory = "R:\07\"
* If Left(Directory, 1) < "\" Then
* * * Directory = Directory & "\"
* End If


* rw = 1


* Set IndexSheet = ThisWorkbook.ActiveSheet


* FileName = Dir(Directory & "*.xls")
* Do While FileName < ""
* * * IndexSheet.Cells(rw, 1).Value = FileName
* * * rw = rw + 1
* * * FileName = Dir
* Loop


* Set IndexSheet = Nothing
End Sub- Hide quoted text -


- Show quoted text -


Bob this is great. I tested your code and viewed the results. Is there
a way to remove the hyperlinks from the filenames? And can I also just
target the excel files in the specific folder "C:\test" and not files
in any subflolders. To be more specific I'd like to only loop through
excel files that begin with "D7"

Thanks so much for your help!
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
File list - list filenames in excel JRP Excel Programming 4 February 19th 07 06:38 PM
List Filenames from Folder Eskimo Excel Programming 1 May 19th 06 08:41 PM
Getting a list of filenames Tom Ogilvy Excel Programming 3 April 13th 05 04:31 PM
Getting a list of filenames Bernie Deitrick Excel Programming 0 April 13th 05 02:55 PM
List out FileNames.xls with K4 Blank JMay Excel Programming 4 December 7th 03 04:35 PM


All times are GMT +1. The time now is 10:13 PM.

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

About Us

"It's about Microsoft Excel"