Solved

Reading files

Posted on 2002-06-12
6
132 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
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now