Results 1 to 21 of 21

Thread: [RESOLVED] Find newest file in Listbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Resolved [RESOLVED] Find newest file in Listbox

    Hey All,

    I'm using FindFilesAPI to search a drive for a specific file, and add the results
    to a Listbox. If there are more than one copy of the same file, I need to
    find the newest one, and it to a textbox.

    The results in List1 are...

    c:\Temp7\TEST0000.INI - 3/13/06 11:04:52 AM
    c:\Temp8\TEST0000.INI - 4/07/05 3:32:52 PM
    c:\Temp9\TEST0000.INI - 6/20/06 9:57:52 AM


    How do I loop through the listbox and find the newest file?

    Thanks in advance,
    Ron

    I'm using VB5
    Last edited by rdcody; Jun 20th, 2006 at 10:20 AM.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find newest file in Listbox

    VB Code:
    1. Dim tmp() As String
    2.     Dim Newest As Date
    3.     For x = 0 To List1.ListCount - 1
    4.         tmp = Split(List1.List(x), " - ")
    5.         If Newest < CDate(tmp(1)) Then
    6.             Newest = CDate(tmp(1))
    7.         End If
    8.     Next
    9.     MsgBox Newest
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Find newest file in Listbox

    Hey Static...I get an error..."Can't assign to array".

    (tmp =) is highlighted.

    I'm using VB5 which doesn't support the split function.... any other ideas?

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find newest file in Listbox

    oooo
    use this code
    then change Split to Split2
    VB Code:
    1. 'Credit goes to Manavo11 ;)
    2. Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
    3.     Dim sParts() As String
    4.     Dim lParts As Long
    5.     Dim lPos As Long
    6.         lPos = InStr(sString, sSeparator)
    7.     While lPos
    8.         ReDim Preserve sParts(lParts)
    9.         sParts(lParts) = Left(sString, lPos - 1)
    10.         sString = Mid(sString, lPos + Len(sSeparator))
    11.         lPos = InStr(sString, sSeparator)
    12.         lParts = lParts + 1
    13.     Wend
    14.     If Len(sString) Then
    15.         ReDim Preserve sParts(lParts)
    16.         sParts(lParts) = sString
    17.         lParts = lParts + 1
    18.     End If
    19.     Split2 = IIf(lParts, sParts, Array())
    20. End Function
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Find newest file in Listbox

    I get the same error

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find newest file in Listbox

    ??? works for me?

    VB Code:
    1. Private Sub WHATEVER()
    2.     Dim tmp() As String
    3.     Dim Newest As Date
    4.     For x = 0 To List1.ListCount - 1
    5.         tmp = [B]Split2[/B](List1.List(x), " - ")
    6.         If Newest < CDate(tmp(1)) Then
    7.             Newest = CDate(tmp(1))
    8.         End If
    9.     Next
    10.     GetNewest = Newest
    11. End Sub
    12.  
    13. 'Credit goes to Manavo11
    14. Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
    15.     Dim sParts() As String
    16.     Dim lParts As Long
    17.     Dim lPos As Long
    18.         lPos = InStr(sString, sSeparator)
    19.     While lPos
    20.         ReDim Preserve sParts(lParts)
    21.         sParts(lParts) = Left(sString, lPos - 1)
    22.         sString = Mid(sString, lPos + Len(sSeparator))
    23.         lPos = InStr(sString, sSeparator)
    24.         lParts = lParts + 1
    25.     Wend
    26.     If Len(sString) Then
    27.         ReDim Preserve sParts(lParts)
    28.         sParts(lParts) = sString
    29.         lParts = lParts + 1
    30.     End If
    31.     Split2 = IIf(lParts, sParts, Array())
    32. End Function
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Find newest file in Listbox

    no.....i won't work in VB5

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find newest file in Listbox

    ... yes it will

    the Split2 Function uses nothing that VB5 doesnt have.

    it was written for people with VB5 so they can have the ease of a split function!?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9

  10. #10
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find newest file in Listbox

    hmm.. never seen that version marty.. looks nice gotta put that one in my codebank.

    but, am I wrong though? the Split2 function should work fine correct?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Find newest file in Listbox

    I still get this error..."Can't assign to array".

    (tmp =) is highlighted.

  12. #12

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Find newest file in Listbox

    Quote Originally Posted by rdcody
    I still get this error..."Can't assign to array".

    (tmp =) is highlighted.
    Are you doing anything in your code other than what has been previously posted?

    I ask, because what Static posted works for me.

  14. #14
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find newest file in Listbox

    what if u change
    DIm tmp() as string
    to
    Dim tmp() as Variant
    ??
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Find newest file in Listbox

    no.....just copied and pasted the code as is.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Find newest file in Listbox

    same error

  17. #17
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find newest file in Listbox

    use this then:
    VB Code:
    1. Private Function GetNewest() As Date
    2.     Dim Newest As Date
    3.     Dim iLoc As Integer
    4.     Dim tmp As String
    5.     For x = 0 To List1.ListCount - 1
    6.  
    7.         iLoc = InStr(List1.List(x), " - ") + 3
    8.         tmp = Mid(List1.List(x), iLoc, Len(List1.List(x)) - iLoc)
    9.         If Newest < CDate(tmp) Then
    10.             Newest = CDate(tmp)
    11.         End If
    12.     Next
    13.     GetNewest = Newest
    14. End Function
    15.  
    16. Private Sub Form_Load()
    17.  
    18. MsgBox GetNewest
    19.  
    20. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Find newest file in Listbox

    Yessss.....that works great Static ...thanks..... :-)


    Now I need to path of the file in a textbox...

    Text4.Text = "c:\Temp9\TEST0000.INI"

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Find newest file in Listbox

    This seems to work to get the path.....

    VB Code:
    1. Dim MyPath As String
    2.  
    3. Private Function GetNewest() As Date
    4.     Dim Newest As Date
    5.     Dim iLoc As Integer
    6.     Dim tmp As String
    7.     For x = 0 To List1.ListCount - 1
    8.  
    9.         iLoc = InStr(List1.List(x), " - ") + 3
    10.        
    11.         tmp = Mid(List1.List(x), iLoc, Len(List1.List(x)) - iLoc)
    12.         If Newest < CDate(tmp) Then
    13.            Newest = CDate(tmp)
    14.            MyPath = Left$(List1.List(x), InStr(1, List1.List(x), " - ") - 1)
    15.         End If
    16.     Next
    17.     GetNewest = Newest
    18. End Function
    19.  
    20. Private Sub Command4_Click()
    21.     MsgBox GetNewest
    22.     Text4.Text = MyPath
    23. End Sub

    Does this work for you guys?
    Last edited by rdcody; Jun 20th, 2006 at 12:32 PM.

  20. #20
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Find newest file in Listbox

    well.. do u need to display the date? or only just pust the path in the textbox of the most recent file?

    if just the path:
    VB Code:
    1. Private Function GetNewest() As String
    2.     Dim Newest As Date
    3.     Dim iLoc As Integer
    4.     Dim tmp As String
    5.     Dim sPath As sting
    6.     For x = 0 To List1.ListCount - 1
    7.  
    8.         iLoc = InStr(List1.List(x), " - ") + 3
    9.        
    10.         tmp = Mid(List1.List(x), iLoc, Len(List1.List(x)) - iLoc)
    11.         If Newest < CDate(tmp) Then
    12.            Newest = CDate(tmp)
    13.            sPath = Left$(List1.List(x), InStr(1, List1.List(x), " - ") - 1)
    14.         End If
    15.     Next
    16.     GetNewest = sPath
    17. End Function
    18.  
    19. Private Sub Command4_Click()
    20.     Text4.Text = GetNewest
    21. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Find newest file in Listbox

    That will do it!

    Thanks alot Static, I appreciate your help.

Posting Permissions

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



Click Here to Expand Forum to Full Width