borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (mm/dd/yy date problem)Next Topic (Referencing objects in loops) New Topic New Poll Post Reply
AndreaVB Forum : VB General : How to retrieve file info
Poster Message
labulabi2
Level: Scholar

Registered: 22-10-2002
Posts: 30

icon How to retrieve file info

Can someone tell me how to retrieve file info, such as file size, attributes, date created, etc? (using VB, of course..)

29-10-2002 at 01:30 AM
View Profile Send Email to User Show All Posts | Quote Reply
Harrism
Level: Guest

icon Re: How to retrieve file info

'Create a form with a command button (command1), a list box (list1)
'and four text boxes (text1, text2, text3 and text4).
'Type in the first textbox a startingpath like c:
'and in the second textbox you put a pattern like *.* or *.txt

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100

Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type
Function StripNulls(OriginalStr As String) As String
    If (InStr(OriginalStr, Chr(0)) > 0) Then
        OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    End If
    StripNulls = OriginalStr
End Function

Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)

    Dim FileName As String ' Walking filename variable...
    Dim DirName As String ' SubDirectory Name
    Dim dirNames() As String ' Buffer for directory name entries
    Dim nDir As Integer ' Number of directories in this path
    Dim i As Integer ' For-loop counter...
    Dim hSearch As Long ' Search Handle
    Dim WFD As WIN32_FIND_DATA
    Dim Cont As Integer
    If Right(path, 1) <> "" Then path = path & ""
    ' Search for subdirectories.
    nDir = 0
    ReDim dirNames(nDir)
    Cont = True
    hSearch = FindFirstFile(path & "*", WFD)
    If hSearch <> INVALID_HANDLE_VALUE Then
        Do While Cont
        DirName = StripNulls(WFD.cFileName)
        ' Ignore the current and encompassing directories.
        If (DirName <> ".") And (DirName <> "..") Then
            ' Check for directory with bitwise comparison.
            If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
                dirNames(nDir) = DirName
                DirCount = DirCount + 1
                nDir = nDir + 1
                ReDim Preserve dirNames(nDir)
            End If
        End If
        Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
        Loop
        Cont = FindClose(hSearch)
    End If
    ' Walk through this directory and sum file sizes.
    hSearch = FindFirstFile(path & SearchStr, WFD)
    Cont = True
    If hSearch <> INVALID_HANDLE_VALUE Then
        While Cont
            FileName = StripNulls(WFD.cFileName)
            If (FileName <> ".") And (FileName <> "..") Then
                FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
                FileCount = FileCount + 1
                List1.AddItem path & FileName
            End If
            Cont = FindNextFile(hSearch, WFD) ' Get next file
        Wend
        Cont = FindClose(hSearch)
    End If
    ' If there are sub-directories...
    If nDir > 0 Then
        ' Recursively walk into them...
        For i = 0 To nDir - 1
            FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "", SearchStr, FileCount, DirCount)
        Next i
    End If
End Function
Sub Command1_Click()
    Dim SearchPath As String, FindStr As String
    Dim FileSize As Long
    Dim NumFiles As Integer, NumDirs As Integer
    Screen.MousePointer = vbHourglass
    List1.Clear
    SearchPath = Text1.Text
    FindStr = Text2.Text
    FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
    Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
    Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
    Screen.MousePointer = vbDefault
End Sub

i think that covers everything.

29-10-2002 at 09:11 AM
| Quote Reply
noycez
Level: VB Guru


Registered: 14-10-2002
Posts: 79
icon Re: How to retrieve file info

Note: File here is the name of your file


To get file date and time:
   strDateTime = FileDateTime(File)

To get bytes:
   strBytes = FileLen(File)

To get File attributes:

strAttrib = GetAttr(File)

If (strAttrib And vbReadOnly) = vbReadOnly Then
  strGetAttrib = "ReadOnlyX"
End If

If (strAttrib And vbHidden) = vbHidden Then
   strGetAttrib= "Hidden"
End If

If (strAttrib And vbSystem) = vbSystem Then
  strGetAttrib = "System"
End If

If (strAttrib And vbArchive) = vbArchive Then
  strGetAttrib = "Archive"
End If



____________________________
Funny thought:

Practice makes perfect.....
But nobody's perfect......
so why practice?

29-10-2002 at 09:25 AM
View Profile Send Email to User Show All Posts | Quote Reply
labulabi2
Level: Scholar

Registered: 22-10-2002
Posts: 30
icon Re: How to retrieve file info

ok, thx guys!  

29-10-2002 at 01:11 PM
View Profile Send Email to User Show All Posts | Quote Reply
arfa
Level: Guest

icon Re: How to retrieve file info

You can use the "GetFileInformationByHandle" function in kernel32.dll

The following code shows you how to do this and is an extract from the API guide which can be obtained from www.allapi.net
---------------------------
Private Const OPEN_EXISTING = 3
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type BY_HANDLE_FILE_INFORMATION
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    dwVolumeSerialNumber As Long
    nFileSizeHigh As Long
    nFileSizeLow As Long
    nNumberOfLinks As Long
    nFileIndexHigh As Long
    nFileIndexLow As Long
End Type
Private Declare Function GetFileInformationByHandle Lib "kernel32" (ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Sub Form_Load()
    'KPD-Team 2001
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@allapi.net
    Dim hFile As Long, FileInfo As BY_HANDLE_FILE_INFORMATION
    'create a handle to the file 'c:autoexec.bat'
    hFile = CreateFile("c:autoexec.bat", 0, 0, ByVal 0&, OPEN_EXISTING, 0, ByVal 0&)
    'retrieve the file information
    GetFileInformationByHandle hFile, FileInfo
    'close the handle
    CloseHandle hFile
    'show the result
    MsgBox "File size: " + CStr(FileInfo.nFileSizeLow), vbInformation
End Sub
------------------------------

Have a look at the guide, it contains a lot of useful functions.

[Edited by admin on 29-10-2002 at 04:05 PM GMT]

29-10-2002 at 02:09 PM
| Quote Reply
labulabi2
Level: Scholar

Registered: 22-10-2002
Posts: 30
icon Re: Re: How to retrieve file info

how to determine a folder, then? is there a folder/directory attribute?

~Labu

quote:
noycez wrote:
Note: File here is the name of your file


To get file date and time:
   strDateTime = FileDateTime(File)

To get bytes:
   strBytes = FileLen(File)

To get File attributes:

strAttrib = GetAttr(File)

If (strAttrib And vbReadOnly) = vbReadOnly Then
  strGetAttrib = "ReadOnlyX"
End If

If (strAttrib And vbHidden) = vbHidden Then
   strGetAttrib= "Hidden"
End If

If (strAttrib And vbSystem) = vbSystem Then
  strGetAttrib = "System"
End If

If (strAttrib And vbArchive) = vbArchive Then
  strGetAttrib = "Archive"
End If


30-10-2002 at 05:53 AM
View Profile Send Email to User Show All Posts | Quote Reply
noycez
Level: VB Guru


Registered: 14-10-2002
Posts: 79
icon Re: Re: Re: How to retrieve file info

*******************************************
Add a DriveListBox, DirListBox and FileListBox controls in your form.Add this codes

Private Sub Dir1_Change()
'to update File1
File1.Path = Dir1
End Sub

Private Sub Drive1_Change()
'to update Dir1 and File1
Dir1.Path = Drive1
File1.Path = Dir1
End Sub

Private Sub Form_Load()
'to display folders in a drive
Dir1.Path = Drive1
'to display the files in the folder
'note that Dir1 is connected to Drive1

File1.Path = Dir1
End Sub

'then to get the selected file in File1 use
'this code
strFile = File1.List(File1.ListIndex)

'or you can do this

getstring =FileDateTime(File1.List(File1.ListIndex)
)

*******************************************


quote:
labulabi2 wrote:
how to determine a folder, then? is there a folder/directory attribute?

~Labu

quote:
noycez wrote:
Note: File here is the name of your file


To get file date and time:
   strDateTime = FileDateTime(File)

To get bytes:
   strBytes = FileLen(File)

To get File attributes:

strAttrib = GetAttr(File)

If (strAttrib And vbReadOnly) = vbReadOnly Then
  strGetAttrib = "ReadOnlyX"
End If

If (strAttrib And vbHidden) = vbHidden Then
   strGetAttrib= "Hidden"
End If

If (strAttrib And vbSystem) = vbSystem Then
  strGetAttrib = "System"
End If

If (strAttrib And vbArchive) = vbArchive Then
  strGetAttrib = "Archive"
End If






____________________________
Funny thought:

Practice makes perfect.....
But nobody's perfect......
so why practice?
30-10-2002 at 07:40 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : How to retrieve file info
Previous Topic (mm/dd/yy date problem)Next Topic (Referencing objects in loops) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder