dcsimg
Results 1 to 5 of 5
  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    320

    Resolved [RESOLVED] List files without FileSystem Object

    Hi all

    I need to get a list of files with details from a certain directory.
    Yes, quite easy using the FileSystem Object.

    I want to do the same without the FileSystem Object.

    In the end, I need to get the youngest file.
    But that's another story.

    Any snippets for such a task around?
    Or other ideas?

    Thank you
    Karl

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,239

    Re: List files without FileSystem Object

    1) VB-native = Dir-Function
    2) API = FindFirstFile (and its sisters)

    @1) Additionally you need to read the Attributes of each file found to get your date
    @2) Since the WIN32_FIND_DATA already contains the necessary date-informations.....
    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    I say you're crazy not to!
    --------------------------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,287

    Re: List files without FileSystem Object

    Yeah, I'd just use the Dir$() function, possibly stuffing it all into a Collection, and then iterating (For Each) the Collection to spit them back out.

    Also, this stuff is all over these forums, but here are some procedures I use when doing this kind of stuff:

    Code:
    Option Explicit
    '
    Private Const MAX_PATH = 260     ' ASCII: 1+2+256+1 or [drive][:\][path][null] = 260, actual [path] = 256.
    '
    Private Type FILETIME
        dwLowDateTime  As Long
        dwHighDateTime As Long
    End Type
    Private Type TwoLongsType
        Long1 As Long
        Long2 As Long
    End Type
    Private 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 Integer
    End Type
    Private Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ' &h01 bit = read only
        ' &h02 bit = hidden
        ' &h04 bit = system
        ' &h10 bit = folder
        ' &h20 bit = archive
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        FileSize As TwoLongsType
        Reserved As TwoLongsType
        'nFileSizeHigh As Long
        'nFileSizeLow As Long
        'dwReserved0 As Long
        'dwReserved1 As Long
        cFileName As String * MAX_PATH ' This places a MAX_PATH limit on the file NAME, but not the entire path.
        cAlternate As String * 14
    End Type
    '
    Private Declare Function FindFirstFileW Lib "kernel32" (ByVal lpFileName As Long, ByVal lpFindFileData As Long) As Long
    Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
    Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
    '
    
    Public Function FileCreatedDate(sFileSpec As String) As Date
        ' If the file doesn't exist, it returns zero (12/30/1899 midnight).
        Dim wfd As WIN32_FIND_DATA
        Dim h As Long
        Const INVALID_HANDLE_VALUE = -1
        '
        h = FindFirstFileW(StrPtr(sFileSpec), VarPtr(wfd))
        If h = INVALID_HANDLE_VALUE Then Exit Function
        '
        FileCreatedDate = dFileTimeToDouble(wfd.ftCreationTime, True)
    End Function
    
    Public Function FileModifiedDate(sFileSpec As String) As Date
        ' If the file doesn't exist, it returns zero (12/30/1899 midnight).
        Dim wfd As WIN32_FIND_DATA
        Dim h As Long
        Const INVALID_HANDLE_VALUE = -1
        '
        h = FindFirstFileW(StrPtr(sFileSpec), VarPtr(wfd))
        If h = INVALID_HANDLE_VALUE Then Exit Function
        '
        FileModifiedDate = dFileTimeToDouble(wfd.ftLastWriteTime, True)
    End Function
    
    Public Function FileAccessedDate(sFileSpec As String) As Date
        ' If the file doesn't exist, it returns zero (12/30/1899 midnight).
        Dim wfd As WIN32_FIND_DATA
        Dim h As Long
        Const INVALID_HANDLE_VALUE = -1
        '
        h = FindFirstFileW(StrPtr(sFileSpec), VarPtr(wfd))
        If h = INVALID_HANDLE_VALUE Then Exit Function
        '
        FileAccessedDate = dFileTimeToDouble(wfd.ftLastAccessTime, True)
    End Function
    
    Private Function dFileTimeToDouble(ftUTC As FILETIME, Optional Localize As Boolean) As Double
       Dim FT As FILETIME
       Dim st As SYSTEMTIME
       Dim d As Double
       Dim t As Double
       ' Convert to local filetime, if necessary.
       If Localize Then
          FileTimeToLocalFileTime ftUTC, FT
       Else
          FT = ftUTC
       End If
       ' Convert to system time structure.
       FileTimeToSystemTime FT, st
       ' Convert to VB-style date (double).
       dFileTimeToDouble = DateSerial(st.wYear, st.wMonth, st.wDay) + TimeSerial(st.wHour, st.wMinute, st.wSecond)
    End Function
    Maybe that'll help you get to where you're trying to go.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    3,638

    Re: List files without FileSystem Object

    Quote Originally Posted by Karl77 View Post
    ...In the end, I need to get the youngest file.
    In case the RC5 is in use, the cDirList class has sorting by different criterions built-in.

    So to find and print the "most recently changed" file in a directory, one can just specify the proper sort-criterion-enum -
    and then directly access the "last entry" (at zerobased index = FilesCount-1) ... or index=0 to get the "oldest" file.
    Code:
      With New_c.FSO.GetDirList("c:\code", dlSortByLastWriteTime)
         If .FilesCount Then Debug.Print .FileName(.FilesCount - 1)
      End With
    Olaf

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    320

    Re: List files without FileSystem Object

    I have it now.
    Too easy with VB6 means, but I must confess I had difficulties with it...

    Code:
    Public Function Files_GetYoungest(ByVal ThePath As String, _
                                      ByVal ThePattern As String) As String
    
    Dim FilesList   As Collection
    Dim DirResult   As String
    
    Dim TimeValue   As Double
    
    Dim i           As Variant
    Dim MinTime     As Double
    Dim MR_FileName As String
    Dim MR_FileDate As Double
    '--------------------------------------------------
    
    If Right$(ThePath, 1) <> "\" Then ThePath = ThePath & "\"
    
    DirResult = Dir$(ThePath & ThePattern)
    Set FilesList = New Collection
    
    If Len(DirResult) > 0 Then
        TimeValue = CDbl(FileDateTime(ThePath & DirResult))
        FilesList.Add Array(TimeValue, DirResult), DirResult
    End If
    
    Do While Len(DirResult) > 0
        DirResult = Dir$
        If Len(DirResult) > 0 Then
            TimeValue = CDbl(FileDateTime(ThePath & DirResult))
            FilesList.Add Array(TimeValue, DirResult), DirResult
        End If
    Loop
    
    
    MinTime = 0
    
    For Each i In FilesList
        'Debug.Print "Files found", i(1)
        TimeValue = CDbl(i(0))
        If TimeValue > MinTime Then
            MinTime = TimeValue
            MR_FileName = i(1)
            MR_FileDate = CDbl(i(0))
        End If
    Next
    
    Files_GetYoungest = MR_FileName
    
    Set FilesList = Nothing
    
    End Function
    Thanks for the kicks
    Karl

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.