Solved

Reading files

Posted on 2002-06-12
6
136 Views
Last Modified: 2010-05-02
How can I scan files in a directory and read in the name and date stamp.
0
Comment
Question by:Maritimer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 5

Expert Comment

by:rpai
ID: 7073471
You could use the FileSystemObject to do that.

Dim fso As Object
Dim f As Object
Dim fc As Object
Dim vFile As File
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set f = fso.GetFolder("C:\Test")
 
  For Each vFile In f.Files
    Debug.Print vkey & vbTab & f.DateLastModified
  Next

Also, f.DateCreated or f.DateLastAccessed could be used depending on what your requirements are.
0
 
LVL 5

Accepted Solution

by:
rpai earned 100 total points
ID: 7073481
The loop should have been:-
 For Each vFile In f.Files
   Debug.Print vFile & vbTab & f.DateLastModified
 Next
0
 
LVL 11

Expert Comment

by:joekendall
ID: 7073512
Here is a site that has the API and FSO approach:

www.mvps.org/vbnet

Search for this topic:

Performance Comparison - FSO vs. API

Thanks!

Joe
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7073776
Option Explicit

'32 bit Windows declarations
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
    (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FileTimeToSystemTime Lib "kernel32" _
    (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Declare Function FileTimeToLocalFileTime Lib "kernel32" _
    (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long

Public Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Public Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Long
End Type

Public 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 * 260
    cAlternate As String * 14
End Type

Private Function FileDate(FT As FILETIME) As String
'   convert the FILETIME to LOCALTIME, then to SYSTEMTIME type
    Dim ST As SYSTEMTIME
    Dim LT As FILETIME
    Dim t As Long
    Dim ds As Double
    Dim ts As Double
    t = FileTimeToLocalFileTime(FT, LT)
    t = FileTimeToSystemTime(LT, ST)
    If t Then
        ds = DateSerial(ST.wYear, ST.wMonth, ST.wDay)
        ts = TimeSerial(ST.wHour, ST.wMinute, ST.wSecond)
        ds = ds + ts
        If ds > 0 Then
            FileDate = Format$(ds, "mm/dd/yy hh:mm:ss")
        Else
            FileDate = "(no date)"
        End If
    End If
End Function

Sub main()
Dim ff As String
Const SOMEPATH As String = "c:\windows\"
ff = Dir$(SOMEPATH & "*.*", vbArchive)
Do While ff <> ""
    ShowFileInfo SOMEPATH & ff
    ff = Dir$()
Loop
End Sub

Private Sub ShowFileInfo(FullName As String)
'   This subroutine demonstrates the technique
    Dim hFile As Long
    Dim WFD As WIN32_FIND_DATA
    Dim Created As String
    Dim LastWrite As String
     
'   FullName is the path and filename
'   Substitute any valid file and path
    hFile = FindFirstFile(FullName, WFD)
   
    If hFile > 0 Then
        Created = FileDate(WFD.ftCreationTime)
        MsgBox "File Created: " & Created, vbInformation, FullName
    Else
        MsgBox "File not found.", vbCritical, FullName
    End If
End Sub
 

0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7073784
oops, sorry, i forgot the name:

Private Sub ShowFileInfo(FullName As String)
'   This subroutine demonstrates the technique
    Dim hFile As Long
    Dim WFD As WIN32_FIND_DATA
    Dim Created As String
    Dim sname As String
    Dim LastWrite As String
     
'   FullName is the path and filename
'   Substitute any valid file and path
    hFile = FindFirstFile(FullName, WFD)
   
    If hFile > 0 Then
        Created = FileDate(WFD.ftCreationTime)
        sname = Left$(WFD.cFileName, InStr(1, WFD.cFileName, Chr$(0), vbTextCompare))
        Debug.Print "File " & sname & "Created: " & Created, vbInformation, FullName
    Else
        MsgBox "File not found.", vbCritical, FullName
    End If
End Sub
0
 

Author Comment

by:Maritimer
ID: 7080320
All the answers were good, however rapi you did answer first and I used your answer. Thanks all.


Maritimer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question