Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Efficient Directory list

Status
Not open for further replies.

MikeLacey

MIS
Nov 9, 1998
13,212
0
0
GB
Hi,<br>
<br>
I'm looking for a way to get a list of files with creation date - sorted by date, most recent first, into a list box.<br>
<br>
I have this working at the moment - but it's a bit inefficient.<br>
<br>
One of the requirements is that the user must be able to click on a file in the list - and be able to edit it. So the refresh of the list shouldn't be an event the user notices.<br>
<br>
The other requirement is that the list should be updatedn &quot;in real time&quot;. I've taken this to mean every couple of seconds.<br>
<br>
My approach so far has been a file list box to pick up all the file names - insert any changed entries into a listbox complete with date and time retrieved with FileDateTime. Not bad - but if you start inserting/updating items in a listbox the Sorted property is worthless and if I start the list off from scratch each time it flicks around and looks horrible.<br>
<br>
So - ideas welcome - Eric? Alt255?<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
look at &quot;Dir&quot; function<br>
here are some samples form Help in VB6<br>
-----------------<br>
Dim MyFile, MyPath, MyName<br>
' Returns &quot;WIN.INI&quot; if it exists.<br>
MyFile = Dir(&quot;C:\WINDOWS\WIN.INI&quot;) <br>
<br>
' Returns filename with specified extension. If more than one *.ini<br>
' file exists, the first file found is returned.<br>
MyFile = Dir(&quot;C:\WINDOWS\*.INI&quot;)<br>
<br>
' Call Dir again without arguments to return the next *.INI file in the <br>
' same directory.<br>
MyFile = Dir<br>
<br>
' Return first *.TXT file with a set hidden attribute.<br>
MyFile = Dir(&quot;*.TXT&quot;, vbHidden)<br>
<br>
' Display the names in C:\ that represent directories.<br>
MyPath = &quot;c:\&quot; ' Set the path.<br>
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.<br>
Do While MyName &lt;&gt; &quot;&quot; ' Start the loop.<br>
' Ignore the current directory and the encompassing directory.<br>
If MyName &lt;&gt; &quot;.&quot; And MyName &lt;&gt; &quot;..&quot; Then<br>
' Use bitwise comparison to make sure MyName is a directory.<br>
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then<br>
Debug.Print MyName ' Display entry only if it<br>
End If ' it represents a directory.<br>
End If<br>
MyName = Dir ' Get next entry.<br>
Loop<br>
<br>

 
you can do it with this Function (i send you the example)<br>
<br>
Private Function LineInfo(fName As String)<br>
'Get more information about each file<br>
Dim nLength As Long<br>
Dim sSpaces As Long<br>
Dim NewEnt As String<br>
Dim DateFix As String<br>
Dim NewDate As String<br>
'Add File Date<br>
DateFix = Str(FileDateTime(fName))<br>
If Mid(DateFix, 2, 1) = &quot;/&quot; Then<br>
'm1<br>
NewDate = &quot;0&quot; + Mid(DateFix, 1, 2)<br>
If Mid(DateFix, 4, 1) = &quot;/&quot; Then<br>
'm1 d1<br>
NewDate = NewDate + &quot;0&quot; + Mid(DateFix, 3, 4) + Space(2)<br>
If Len(DateFix) &lt; 9 Then<br>
'm1 d1 Midnight<br>
NewDate = NewDate + &quot;12:00:00 AM&quot;<br>
End If<br>
If Mid(DateFix, 9, 1) = &quot;:&quot; Then<br>
'm1 d1 h1<br>
NewDate = NewDate + &quot;0&quot; + Mid(DateFix, 8, 17)<br>
Else<br>
'm1 d1 h2<br>
NewDate = NewDate + Mid(DateFix, 8, 18)<br>
End If<br>
Else<br>
'm1 d2<br>
NewDate = NewDate + Mid(DateFix, 3, 5) + Space(2)<br>
If Len(DateFix) &lt; 9 Then<br>
'm1 d2 Midnight<br>
NewDate = NewDate + &quot;12:00:00 AM&quot;<br>
End If<br>
If Mid(DateFix, 10, 1) = &quot;:&quot; Then<br>
'm1 d2 h1<br>
NewDate = NewDate + &quot;0&quot; + Mid(DateFix, 9, 17)<br>
Else<br>
'm1 d2 h2<br>
NewDate = NewDate + Mid(DateFix, 9, 18)<br>
End If<br>
End If<br>
Else<br>
'm2<br>
NewDate = Mid(DateFix, 1, 3)<br>
If Mid(DateFix, 5, 1) = &quot;/&quot; Then<br>
'm2 d1<br>
NewDate = NewDate + &quot;0&quot; + Mid(DateFix, 4, 4) + Space(2)<br>
If Len(DateFix) &lt; 9 Then<br>
'm2 d1 Midnight<br>
NewDate = NewDate + &quot;12:00:00 AM&quot;<br>
End If<br>
If Mid(DateFix, 10, 1) = &quot;:&quot; Then<br>
'm2 d1 h1<br>
NewDate = NewDate + &quot;0&quot; + Mid(DateFix, 9, 17)<br>
Else<br>
'm2 d1 h2<br>
NewDate = NewDate + Mid(DateFix, 9, 18)<br>
End If<br>
Else<br>
'm2 d2<br>
NewDate = NewDate + Mid(DateFix, 4, 5) + Space(2)<br>
If Len(DateFix) &lt; 9 Then<br>
'm2 d2 Midnight<br>
NewDate = NewDate + &quot;12:00:00 AM&quot;<br>
End If<br>
If Mid(DateFix, 11, 1) = &quot;:&quot; Then<br>
'm2 d2 h1<br>
NewDate = NewDate + &quot;0&quot; + Mid(DateFix, 10, 17)<br>
Else<br>
'm2 d2 h2<br>
NewDate = NewDate + Mid(DateFix, 10, 18)<br>
End If<br>
End If<br>
End If<br>
LineInfo = NewDate + Space(2)<br>
'Add File Length<br>
nLength = Len(Str(FileLen(fName)))<br>
sSpaces = 10 - nLength<br>
LineInfo = LineInfo + Space(sSpaces) + Str(FileLen(fName))<br>
List1.AddItem LineInfo<br>
End Function<br>
<p>Eric De Decker<br><a href=mailto:vbg.be@vbgroup.nl>vbg.be@vbgroup.nl</a><br><a href= Basic Center</a><br>
 
Mike, it sounds like you've got it made (except for the problem with the horrible update flicker). Why not store the results of your file search in an invisible object, compare the contents against your list box and only refresh the list box when there have been changes? It would eliminate the flicker and keep the program workings behind the scenes, where we like them.<br>
I'd show you a quick way to do this but I would surely draw the ridicule of the programming &quot;purists&quot; out there.<br>
<br>
Purity is in the head, functionality is in the hand.
 
Alt - that's actually what I did &lt;smile&gt; probably in a way that would make the real programmers feel ill.<br>
<br>
I like Eric's solution better though (it's prettier) so I'm going to use that.<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top