|
-
Jun 20th, 2006, 10:15 AM
#1
Thread Starter
Addicted Member
[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.
-
Jun 20th, 2006, 10:21 AM
#2
Re: Find newest file in Listbox
VB Code:
Dim tmp() As String
Dim Newest As Date
For x = 0 To List1.ListCount - 1
tmp = Split(List1.List(x), " - ")
If Newest < CDate(tmp(1)) Then
Newest = CDate(tmp(1))
End If
Next
MsgBox Newest
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 20th, 2006, 10:33 AM
#3
Thread Starter
Addicted Member
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?
-
Jun 20th, 2006, 10:48 AM
#4
Re: Find newest file in Listbox
oooo 
use this code
then change Split to Split2
VB Code:
'Credit goes to Manavo11 ;)
Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
Dim sParts() As String
Dim lParts As Long
Dim lPos As Long
lPos = InStr(sString, sSeparator)
While lPos
ReDim Preserve sParts(lParts)
sParts(lParts) = Left(sString, lPos - 1)
sString = Mid(sString, lPos + Len(sSeparator))
lPos = InStr(sString, sSeparator)
lParts = lParts + 1
Wend
If Len(sString) Then
ReDim Preserve sParts(lParts)
sParts(lParts) = sString
lParts = lParts + 1
End If
Split2 = IIf(lParts, sParts, Array())
End Function
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 20th, 2006, 10:51 AM
#5
Thread Starter
Addicted Member
Re: Find newest file in Listbox
-
Jun 20th, 2006, 11:10 AM
#6
Re: Find newest file in Listbox
??? works for me?
VB Code:
Private Sub WHATEVER()
Dim tmp() As String
Dim Newest As Date
For x = 0 To List1.ListCount - 1
tmp = [B]Split2[/B](List1.List(x), " - ")
If Newest < CDate(tmp(1)) Then
Newest = CDate(tmp(1))
End If
Next
GetNewest = Newest
End Sub
'Credit goes to Manavo11
Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
Dim sParts() As String
Dim lParts As Long
Dim lPos As Long
lPos = InStr(sString, sSeparator)
While lPos
ReDim Preserve sParts(lParts)
sParts(lParts) = Left(sString, lPos - 1)
sString = Mid(sString, lPos + Len(sSeparator))
lPos = InStr(sString, sSeparator)
lParts = lParts + 1
Wend
If Len(sString) Then
ReDim Preserve sParts(lParts)
sParts(lParts) = sString
lParts = lParts + 1
End If
Split2 = IIf(lParts, sParts, Array())
End Function
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 20th, 2006, 11:16 AM
#7
Thread Starter
Addicted Member
Re: Find newest file in Listbox
no.....i won't work in VB5
-
Jun 20th, 2006, 11:31 AM
#8
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"
-
Jun 20th, 2006, 11:32 AM
#9
Re: Find newest file in Listbox
Here is a version of Split for VB5.
-
Jun 20th, 2006, 11:35 AM
#10
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"
-
Jun 20th, 2006, 11:41 AM
#11
Thread Starter
Addicted Member
Re: Find newest file in Listbox
I still get this error..."Can't assign to array".
(tmp =) is highlighted.
-
Jun 20th, 2006, 11:45 AM
#12
Re: Find newest file in Listbox
It does work in VB6 but I don't know about VB5 which may be different in the way it handles arrays.
-
Jun 20th, 2006, 11:46 AM
#13
Re: Find newest file in Listbox
 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.
-
Jun 20th, 2006, 11:48 AM
#14
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"
-
Jun 20th, 2006, 11:49 AM
#15
Thread Starter
Addicted Member
Re: Find newest file in Listbox
no.....just copied and pasted the code as is.
-
Jun 20th, 2006, 11:51 AM
#16
Thread Starter
Addicted Member
Re: Find newest file in Listbox
-
Jun 20th, 2006, 11:55 AM
#17
Re: Find newest file in Listbox
use this then:
VB Code:
Private Function GetNewest() As Date
Dim Newest As Date
Dim iLoc As Integer
Dim tmp As String
For x = 0 To List1.ListCount - 1
iLoc = InStr(List1.List(x), " - ") + 3
tmp = Mid(List1.List(x), iLoc, Len(List1.List(x)) - iLoc)
If Newest < CDate(tmp) Then
Newest = CDate(tmp)
End If
Next
GetNewest = Newest
End Function
Private Sub Form_Load()
MsgBox GetNewest
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 20th, 2006, 12:03 PM
#18
Thread Starter
Addicted Member
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"
-
Jun 20th, 2006, 12:27 PM
#19
Thread Starter
Addicted Member
Re: Find newest file in Listbox
This seems to work to get the path.....
VB Code:
Dim MyPath As String
Private Function GetNewest() As Date
Dim Newest As Date
Dim iLoc As Integer
Dim tmp As String
For x = 0 To List1.ListCount - 1
iLoc = InStr(List1.List(x), " - ") + 3
tmp = Mid(List1.List(x), iLoc, Len(List1.List(x)) - iLoc)
If Newest < CDate(tmp) Then
Newest = CDate(tmp)
MyPath = Left$(List1.List(x), InStr(1, List1.List(x), " - ") - 1)
End If
Next
GetNewest = Newest
End Function
Private Sub Command4_Click()
MsgBox GetNewest
Text4.Text = MyPath
End Sub
Does this work for you guys?
Last edited by rdcody; Jun 20th, 2006 at 12:32 PM.
-
Jun 20th, 2006, 01:31 PM
#20
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:
Private Function GetNewest() As String
Dim Newest As Date
Dim iLoc As Integer
Dim tmp As String
Dim sPath As sting
For x = 0 To List1.ListCount - 1
iLoc = InStr(List1.List(x), " - ") + 3
tmp = Mid(List1.List(x), iLoc, Len(List1.List(x)) - iLoc)
If Newest < CDate(tmp) Then
Newest = CDate(tmp)
sPath = Left$(List1.List(x), InStr(1, List1.List(x), " - ") - 1)
End If
Next
GetNewest = sPath
End Function
Private Sub Command4_Click()
Text4.Text = GetNewest
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jun 20th, 2006, 02:14 PM
#21
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|