PDA

View Full Version : VBA to get details of files in a folder



andygill
05-17-2010, 03:44 AM
Hi

I'm using some VBA code to get information on files in a folder.

for example.

Cells(r, 2).Formula = FileItem.Size
Cells(r, 3).Formula = FileItem.Type
Cells(r, 4).Formula = FileItem.DateCreated
Cells(r, 5).Formula = FileItem.DateLastAccessed

Is it possible to add the owner of the file.

Thanks
Andy

GTO
05-17-2010, 03:49 AM
Greetings and welcome to VBAX.

I am probably being thick-headed, but what is FileItem, or where did it come from? FSO, Filesearch?

andygill
05-17-2010, 04:01 AM
Dim FileItem As Scripting.File
Dim r As Long
Set FSO = New Scripting.FileSystemObject
Set SourceFolder = FSO.GetFolder(SourceFolderName)
r = Range("A65536").End(xlUp).Row + 1
For Each FileItem In SourceFolder.Files
' display file properties
Cells(r, 1).Formula = FileItem.Path & FileItem.Name
Cells(r, 2).Formula = FileItem.Size
Cells(r, 3).Formula = FileItem.Type
Cells(r, 4).Formula = FileItem.DateCreated
Cells(r, 5).Formula = FileItem.DateLastAccessed
Cells(r, 6).Formula = FileItem.DateLastModified
Cells(r, 7).Formula = FileItem.Attributes
Cells(r, 8).Formula = FileItem.ShortPath & FileItem.ShortName
'''Cells(r, 9).Formula = FileItem.OwnerID
' use file methods (not proper in this example)
' FileItem.Copy "C:\FolderName\Filename.txt", True
' FileItem.Move "C:\FolderName\Filename.txt"
' FileItem.Delete True
r = r + 1 ' next row number
Next FileItem

GTO
05-17-2010, 04:27 AM
Look at Shell and NameSpace here:

http://www.vbaexpress.com/kb/getarticle.php?kb_id=405

Hope that helps,

Mark

Bob Phillips
05-17-2010, 04:28 AM
You can't get it from FSO, but there is an article here (http://support.microsoft.com/kb/218965/en-gb)showing how.