UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Use VBA to get File Information from Directory    
 
   
SerranoG
post Nov 29 2006, 09:53 AM
Post#1


UtterAccess VIP
Posts: 2,219
Joined: 1-December 03
From: Lansing, MI USA


I'm looking for the correct syntax to get info about files in a directory. So far all the answers I have found use code that only retreive file name (that's it); or if it has a bit more, compiling it yields a missing library reference and the code has no meniton about which library must be present to run it... and it still doesn't tell me syntax to get date, size, and owner.
On a nutshell, I want this:
Ogive it a drive, a directory, an optional file spec (e.g. *.txt), and a boolean (TRUE means include subfolders).
It gives me a list (preferably in a table) of the path, file name, file size, file date, and the owner (see attached for example).
Thank you in advance.
Go to the top of the page
 
freakazeud
post Nov 29 2006, 10:00 AM
Post#2


UtterAccess VIP
Posts: 31,413
Joined: 23-September 04
From: NC, USA


Hi,
you will need to use the FileSystem object for that.
You can read through this discussion for some good pointers to start.
HTH
Good luck
Go to the top of the page
 
CyberCow
post Nov 29 2006, 10:57 AM
Post#3


UdderAccess Admin + UA Ruler
Posts: 19,557
Joined: 27-April 02
From: Upper MI


The code in this example may well provide what you are looking for. There is one lib ref to the Windows Scripting Runtime that is not standard, but it exists in the system32 directory, so there should be no trouble pointing to it.
Hope this helps
Go to the top of the page
 
SerranoG
post Nov 29 2006, 03:39 PM
Post#4


UtterAccess VIP
Posts: 2,219
Joined: 1-December 03
From: Lansing, MI USA


Thanks, gentlemen. Cyber, that looks promising. I didn't dig deep yet, but I hope I can extract OWNER as well. Oli, you're right in that thread giving me clues. It brings up one question. Where the heck does one go to find out what methods exist, e.g. after the fso? For example, FileExist, .File, .DateCreated, etc.? That's the problem for me. I know what I want, I just don't have any idea what Access calls it! Is there a list somewhere?
Go to the top of the page
 
SerranoG
post Nov 29 2006, 04:30 PM
Post#5


UtterAccess VIP
Posts: 2,219
Joined: 1-December 03
From: Lansing, MI USA


At first try, this looks promising! As feared, the file system object didn't have an OWNER property to add to the code. Adding the option to specify a filespec would be a nice addition, e.g. "*.txt". If I do it, I'll send it back to you.
Cool stuff! cool.gif
Go to the top of the page
 
Corky
post Nov 30 2006, 06:46 AM
Post#6



Posts: 358
Joined: 22-August 02
From: NE PA


This function may be what you are looking for.
Osearched the arhive for this snippet I copied months / years ago?, but I couldn't find it so I'm not sure who to credit it to.
ublic Function GetFileInfo(FileName As String, Optional fReturn As String) As String
On Error GoTo err_fInfo
Dim myFSO, myFileObject

Dim lngAttributes As Long 'Attribute Values, explanation below
Dim datCreated As Date 'Date and time the file was created.
Dim datLastAccessed As Date 'Date and time the file was last accessed.
Dim datLastModified As Date 'Date and time the file was last modified.
Dim strDrive As String 'Drive letter of the drive where the file resides.
Dim strName As String 'Sets or returns the name of the file.
Dim strPath As String 'The path of the file, including drive letter.
Dim strSize As String 'The size, in bytes, of the file.

'*)Component values of the Attribute property. Attribute Value Description Access
'Normal 0 Normal file; no attributes are set
'ReadOnly 1 Read-only file Read/write
'Hidden 2 Hidden file Read/write
'System 4 System file Read/write
'Volume 8 Disk drive volume label Read-only
'Directory 16 Folder or directory Read-only
'Archive 32 File has changed since last backup Read/write
'Alias 64 Link or shortcut Read-only
'Compressed 128 Compressed file Read-only

Set myFSO = CreateObject("Scripting.FileSystemObject")
Set myFileObject = myFSO.GetFile(FileName)

If fReturn = "" Then
With myFileObject
lngAttributes = .Attributes
datCreated = .DateCreated
datLastAccessed = .dateLastAccessed
datLastModified = .dateLastModified
strDrive = .Drive
strName = .Name
strPath = .path
strSize = .Size
End With
' return all file info as string
GetFileInfo = "File Information for file:" & vbCrLf & vbCrLf & _
"Filename: " & strName & vbCrLf & _
"Attributes: " & lngAttributes & vbCrLf & _
"Date Created: " & datCreated & vbCrLf & _
"Date last Accessed: " & datLastAccessed & vbCrLf & _
"Date Last Modified: " & datLastModified & vbCrLf & _
"Drive: " & strDrive & vbCrLf & _
"Path: " & strPath & vbCrLf & _
"Size: " & strSize & " Bytes"
Else
Dim vReturn As Variant
Select Case fReturn
Case "DateCreated"
vReturn = myFileObject.DateCreated
Case "DateLastMod"
vReturn = myFileObject.dateLastModified
Case "DateLastAccessed"
vReturn = myFileObject.dateLastAccessed
Case "Size"
vReturn = myFileObject.Size & " Bytes"
Case Else
vReturn = myFileObject.Name
End Select
GetFileInfo = vReturn
End If

Exit_err:
Set myFileObject = Nothing
Set myFSO = Nothing
Exit Function
err_fInfo:
GetFileInfo = Err.Description
Resume Exit_err
End Function ' GetFileInfo
Go to the top of the page
 
SerranoG
post Dec 1 2006, 09:00 AM
Post#7


UtterAccess VIP
Posts: 2,219
Joined: 1-December 03
From: Lansing, MI USA


Thanks, Corky. Your code is similar to CyberCow's example. Alas, though... no way to get OWNER either. :(
Go to the top of the page
 
freakazeud
post Dec 1 2006, 09:34 AM
Post#8


UtterAccess VIP
Posts: 31,413
Joined: 23-September 04
From: NC, USA


Hi,
I think if you don't use late binding and set a reference to the Microsoft Runtime Scripting you can use intellisense to find out all the properties while typing...then you can use the object browser (vba editor--view--object browser...) to look up details about the properties you find. You could of course also search the web for a complete reference which exposes all its properties.
I don't think the File System Object has a property to retrieve the owner of a file.
However I think you can achieve this by automating the WMI's Win32_LogicalFileOwner class. A good sample of this can be found
here!
You could use that in combination with the file system object to achieve what you want.
HTH
Good luck
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    23rd April 2019 - 05:03 AM