News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

File Attributes

Started by Robert Collins, January 19, 2005, 04:15:12 AM

Previous topic - Next topic

Robert Collins

I need to get the file size and file date. How can I do this without doing anything with the file itself? Using name only.

00100b

I don't know about "without doing anything with the file itself".  The OpenFile Win32 API call will obtain a handle to the file that can be used with the GetFileTime Win32 API call (coupled with the FileTimeToSystemTime Win32 API calls can be used to obtain the Creation Time, Last Accessed Time, and Last Write Time values for a file) and the GetFileSize Win32 API call to obtain the size of the file.  Remember to close the handle to the file when you are done with it using the CloseHandle Win32 API call.

You can also call the FindFirstFile Win32 API call which will populate a WIN32_FIND_DATA structure with both the size and time stamps.  This does not require the opening of a handle to the file itself, but instead creates a search handle that will need to be closed using the FindClose Win32 API call.

Robert Collins

Thanks 00100b, I think you have solved my problem or at least put me on the right track.

I am building a directory and file list in a table for an FTP server application to be sent as the response of the LIST command and I don't want to have to open every file in the directory just to obtain it's size and creation date.

00100b

Then yes, I would recommend the FindFirstFile, FindNextFile Win32 API calls.  Together, they will allow you to iterate through the files of a directory and obtain that inforamtion.

Robert Collins

OK, here's the way to do it.

I will give a VB example but I'm sure the same thing will work in other languages.


Function GetFileInformation()

Dim filespec As String

On Local Error GoTo GetFileInfo_Err


filespec = "C:\MyFile.txt"

Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
 
'
' Settings for .Attributes argument
'
' The attributes argument can have any of the following values
' or any logical combination of the following values:
'
' Constant Value Description:
'
' 0    Normal file. No attributes are set.
' 1    Read-only file. Attribute is read/write.
' 2    Hidden file. Attribute is read/write.
' 4    System file. Attribute is read/write.
' 8    Disk drive volume label. Attribute is read-only.
' 16   Folder or directory. Attribute is read-only.
' 32   Archive. File has changed since last backup. Attribute is read/write.
' 1024 Alias. Link or shortcut. Attribute is read-only.
' 2048 Compressed file. Attribute is read-only.
'
MsgBox "File: " & f.Name & vbCrLf & vbCrLf & _
        "Attributes = " & f.Attributes & vbCrLf & _
        "Type = " & f.Type & vbCrLf & _
        "Size = " & f.Size & vbCrLf & _
        "Date Created = " & f.DateCreated & vbCrLf & _
        "Date Last Accessed = " & f.DateLastAccessed & vbCrLf & _
        "Date Last Modified = " & f.DateLastModified & vbCrLf & _
        "Short Path = " & f.ShortPath
 
Set f = Nothing
Set fso = Nothing

Exit Function
 
GetFileInfo_Err:
MsgBox "Error = " & Err.Description
End Function


You can go to below link to learn all about this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsobjfilesystem.asp