?
Solved

User must select from list of files sorted by DateLastModified

Posted on 2003-03-24
17
Medium Priority
?
282 Views
Last Modified: 2008-03-04
Hello experts.

I need some help with file manipulation in VB6.

(Note: I would like to accomplish this task without using FileListBox)

Directory c:\StoreFiles contains from 0 to 10 (varies) files all with different primary filenames but sharing a common file extension ".01". These are comma-delineated sequential text files.

File names must be presented to user in something like a ListBox sorted by DateLastModified from oldest to newest, with the oldest already highlighted. User must select the oldest, and when this event occurs I'd llke to present a view if the file (multiline TextBox or whatever) so that user can see contents in its native ASCII format. After user acknowledges in a MsgBOX he/she wishes to continue processing the file, I'll input the file into fields of a database table (I've been able to read any of these files into DB successfully when I give the Input statement a unique filename). The file that was selected and processed must then be moved to directory c:\OldFiles, and must be removed from the ListBox. Now the ListBox contains one less file, but is still sorted with oldest first and highlighted. This process repeats until all the files have been processed.

I've examined several previous posts about API and sorting, but haven't been able to easily:

1) Read C:\StoreFiles for all filenames with .01 extension, then
2) Present the files to user for selection sorted with oldest file first and highlighted, then
3)Move the file after processing to c:\OldFiles and remove it from the ListBox (or whatever control is best for presenting, selecting).

Again, I would like to avoid a hiden or visible FileListBox, which I undestand to be an aged or old tool. Thanks in advance.
0
Comment
Question by:LarryZ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
17 Comments
 
LVL 1

Expert Comment

by:hmadhur
ID: 8195265
Hi,
The best option for you is to use FileSystemObject(FSO). You will get this in Microsoft Scripting Runtime (SCRRUN.DLL). You can use this FileSystemObject to get the Folder Listing, Files in the Folder, and you can easily copy, delete, move files to and fro from one folder to another.

But to sort the files I dont know whether FSO can handle or not. I will check out and get back to you. In the meantime, you can have a look at FSO.

Regards
Madhur.
0
 
LVL 1

Expert Comment

by:hmadhur
ID: 8195273
Hi,
The best option for you is to use FileSystemObject(FSO). You will get this in Microsoft Scripting Runtime (SCRRUN.DLL). You can use this FileSystemObject to get the Folder Listing, Files in the Folder, and you can easily copy, delete, move files to and fro from one folder to another.

But to sort the files I dont know whether FSO can handle or not. I will check out and get back to you. In the meantime, you can have a look at FSO.

Regards
Madhur.
0
 
LVL 3

Expert Comment

by:DocM
ID: 8195393
Hi,

Using a collection and 2 ListBox controls:

' List1 Sorted property set to True
' List2 Sorted property set to False


Dim c As New Collection

Private Function GetDirToListBox(mypath, strExtension)
    For Num = 1 To c.Count                                 ' Remove name from the collection.
        c.Remove 1                                         ' Since collections are reindexed
' automatically, remove the first
    Next                                                   ' member on each iteration.

    List1.Clear
    filter = mypath & strExtension
    myname = Dir(filter)                                   ' Retrieve the first entry.

    Do While myname <> ""                                  ' Start the loop.
        DateModified = FileDateTime(mypath & myname)

        List1.AddItem DateModified
        List1.ItemData(List1.NewIndex) = List1.NewIndex

        c.Add Item:=myname, Key:=CStr(List1.NewIndex)

        myname = Dir                                       ' Get next entry.
    Loop

End Function

Private Sub Command1_Click()
    GetDirToListBox "C:\", "*.txt"
End Sub

Private Sub Command2_Click()
    List2.Clear
    Debug.Print "-------------"
    For i = List1.ListCount - 1 To 0 Step -1
        List2.AddItem c.Item(CStr(List1.ItemData(i)))
       ' List3.AddItem List1.List(i) & " - " & c.Item(CStr(List1.ItemData(i)))
    Next
End Sub

 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:LarryZ
ID: 8195731
Hello DocM

I'm trying to implement your routines now but not sure I'm doing it right.

In the General Declarations section of Form1 I declared the vars with:
  Dim c As New Collection
  Dim myname
  Dim mypath
  Dim i As Integer
  Dim strExtension As String


In Command1_click I changed your  GetDirToListBox "C:\", "*.txt"
to my
 GetDirToListBox "C:\StoreFiles", "*.01"


If I try to Run with Full Compile, at the statement:

     Filter = mypath & strExtension

I get the Compile Error, Argument Not Optional error.

Don't mypath and strExtension need to be assigned values somewhere?

Thanks for help so far
0
 

Author Comment

by:LarryZ
ID: 8196086
To DocM:

I'm getting closer now.

I added two more variable declarations:
  Dim filter
  Dim DateModified

Then I changed your
    filter = mypath & strExtension
to
    filter = mypath & "\" & strExtension

and your
    DateModified = FileDateTime(mypath & myname)
to
    DateModified = FileDateTime(mypath & "\" & myname)

Now it fills List1 until it can't find any more files with the .01 extension, and crashes with
  "Run time Error 457"
  "This key is already associated with an element of this  collection"





0
 
LVL 3

Expert Comment

by:DocM
ID: 8196704
Do not forget to empty the collection first.

But the collection is not necessary. Here is an example with the mid() function to get the same results.


Private Function GetDirToListBox(mypath, strExtension)
   
    Dim filter As String
    Dim myname As String
    Dim DateModified As Date
    Dim i As Integer
   
    If Right(mypath, 1) <> "\" Then mypath = mypath & "\"

    List1.Clear
   
    filter = mypath & strExtension
    myname = Dir(filter)  ' Retrieve the first entry.

    Do While myname <> ""          ' Start the loop.
        DateModified = FileDateTime(mypath & myname)

        List1.AddItem DateModified & " " & myname
        List1.ItemData(List1.NewIndex) = List1.NewIndex

        myname = Dir   ' Get next entry.
    Loop

    List2.Clear

    For i = List1.ListCount - 1 To 0 Step -1
        List2.AddItem Mid(List1.List(i), 21)
    Next

End Function

Private Sub Command1_Click()
    GetDirToListBox "C:\", "*.txt"
End Sub
0
 

Author Comment

by:LarryZ
ID: 8196781
To DocM:

We're getting really close here. Thanks for the alternate code, but before we give up on the collection approach, could you explain how and where in code to empty the collection? I've not had much experience with them. I notice that if I get the Runtime 457 error, comment it out in the break, then press Command2, i do get the filenames in List2, but the chronological order wasn't right..

Oldest
Newest
Next Newest
etc.

Can you help me to empty the collection and try it again?

Thanks

0
 

Author Comment

by:LarryZ
ID: 8196936
To DocM:

I'm single-stepping through the code. Here's what I think I am seeing...

The Do While loop in GetDirToListBox reads the dates for the files as they are read by the Dir, no particular order except the order they reside in directory. They show up in List1.

Then, Command2_click event seems to just invert their order in List2 instead of sorting by date???? Or maybe it's just acting that way because the collection isn't cleared as you suggested.
0
 
LVL 3

Expert Comment

by:DocM
ID: 8197178
Do not forget to empty the collection first.

But the collection is not necessary. Here is an example with the mid() function to get the same results.


Private Function GetDirToListBox(mypath, strExtension)
   
    Dim filter As String
    Dim myname As String
    Dim DateModified As Date
    Dim i As Integer
   
    If Right(mypath, 1) <> "\" Then mypath = mypath & "\"

    List1.Clear
   
    filter = mypath & strExtension
    myname = Dir(filter)  ' Retrieve the first entry.

    Do While myname <> ""          ' Start the loop.
        DateModified = FileDateTime(mypath & myname)

        List1.AddItem DateModified & " " & myname
        List1.ItemData(List1.NewIndex) = List1.NewIndex

        myname = Dir   ' Get next entry.
    Loop

    List2.Clear

    For i = List1.ListCount - 1 To 0 Step -1
        List2.AddItem Mid(List1.List(i), 21)
    Next

End Function

Private Sub Command1_Click()
    GetDirToListBox "C:\", "*.txt"
End Sub
0
 
LVL 3

Expert Comment

by:DocM
ID: 8197201

Option Explicit

Dim c As New Collection

Private Function GetDirToListBox(mypath, strExtension)
Dim num As Long
Dim filter As String
Dim myname  As String
Dim DateModified As Date
Dim i As Integer
Dim intN As Integer

'To empty the collection
   For num = 1 To c.Count     ' Remove name from the collection.
       c.Remove 1             ' Since collections are reindexed
                              ' automatically, remove the first
   Next                       ' member on each iteration.

   List1.Clear
   
   If Right(mypath, 1) <> "\" Then mypath = mypath & "\"

   filter = mypath & strExtension
   myname = Dir(filter)                                   ' Retrieve the first entry.

   Do While myname <> ""                                  ' Start the loop.
       DateModified = FileDateTime(mypath & myname)

       List1.AddItem DateModified & " " & myname
       'A different value for each itemdata  
       intN = intN + 1
       List1.ItemData(List1.NewIndex) = intN
       'List1.itemdata for this item must be the same as the Key associated with myname
       c.Add Item:=myname, Key:=CStr(intN)

       myname = Dir    ' Get next entry.
   Loop
   
List2.Clear
   
   For i = List1.ListCount - 1 To 0 Step -1
       List2.AddItem c.Item(CStr(List1.ItemData(i)))
   Next
   
End Function

Private Sub Command1_Click()
   GetDirToListBox "C:\", "*.txt"
End Sub

0
 

Author Comment

by:LarryZ
ID: 8197202
To DocM:

I'm single-stepping through the code. Here's what I think I am seeing...

The Do While loop in GetDirToListBox reads the dates for the files as they are read by the Dir, no particular order except the order they reside in directory. They show up in List1.

Then, Command2_click event seems to just invert their order in List2 instead of sorting by date???? Or maybe it's just acting that way because the collection isn't cleared as you suggested.
0
 

Author Comment

by:LarryZ
ID: 8197236
To DocM:

I'm single-stepping through the code. Here's what I think I am seeing...

The Do While loop in GetDirToListBox reads the dates for the files as they are read by the Dir, no particular order except the order they reside in directory. They show up in List1.

Then, Command2_click event seems to just invert their order in List2 instead of sorting by date???? Or maybe it's just acting that way because the collection isn't cleared as you suggested.
0
 
LVL 3

Expert Comment

by:DocM
ID: 8197245

Option Explicit

Dim c As New Collection

Private Function GetDirToListBox(mypath, strExtension)
Dim num As Long
Dim filter As String
Dim myname  As String
Dim DateModified As Date
Dim i As Integer
Dim intN As Integer

'To empty the collection
   For num = 1 To c.Count     ' Remove name from the collection.
       c.Remove 1             ' Since collections are reindexed
                              ' automatically, remove the first
   Next                       ' member on each iteration.

   List1.Clear
   
   If Right(mypath, 1) <> "\" Then mypath = mypath & "\"

   filter = mypath & strExtension
   myname = Dir(filter)                                   ' Retrieve the first entry.

   Do While myname <> ""                                  ' Start the loop.
       DateModified = FileDateTime(mypath & myname)

       List1.AddItem DateModified & " " & myname
       'A different value for each itemdata  
       intN = intN + 1
       List1.ItemData(List1.NewIndex) = intN
       'List1.itemdata for this item must be the same as the Key associated with myname
       c.Add Item:=myname, Key:=CStr(intN)

       myname = Dir    ' Get next entry.
   Loop
   
List2.Clear
   
   For i = List1.ListCount - 1 To 0 Step -1
       List2.AddItem c.Item(CStr(List1.ItemData(i)))
   Next
   
End Function

Private Sub Command1_Click()
   GetDirToListBox "C:\", "*.txt"
End Sub

0
 
LVL 3

Expert Comment

by:DocM
ID: 8197262

Option Explicit

Dim c As New Collection

Private Function GetDirToListBox(mypath, strExtension)
Dim num As Long
Dim filter As String
Dim myname  As String
Dim DateModified As Date
Dim i As Integer
Dim intN As Integer

'To empty the collection
   For num = 1 To c.Count     ' Remove name from the collection.
       c.Remove 1             ' Since collections are reindexed
                              ' automatically, remove the first
   Next                       ' member on each iteration.

   List1.Clear
   
   If Right(mypath, 1) <> "\" Then mypath = mypath & "\"

   filter = mypath & strExtension
   myname = Dir(filter)                                   ' Retrieve the first entry.

   Do While myname <> ""                                  ' Start the loop.
       DateModified = FileDateTime(mypath & myname)

       List1.AddItem DateModified & " " & myname
       'A different value for each itemdata  
       intN = intN + 1
       List1.ItemData(List1.NewIndex) = intN
       'List1.itemdata for this item must be the same as the Key associated with myname
       c.Add Item:=myname, Key:=CStr(intN)

       myname = Dir    ' Get next entry.
   Loop
   
List2.Clear
   
   For i = List1.ListCount - 1 To 0 Step -1
       List2.AddItem c.Item(CStr(List1.ItemData(i)))
   Next
   
End Function

Private Sub Command1_Click()
   GetDirToListBox "C:\", "*.txt"
End Sub

0
 
LVL 3

Accepted Solution

by:
DocM earned 200 total points
ID: 8197271

Option Explicit

Dim c As New Collection

Private Function GetDirToListBox(mypath, strExtension)
Dim num As Long
Dim filter As String
Dim myname  As String
Dim DateModified As Date
Dim i As Integer
Dim intN As Integer

'To empty the collection
   For num = 1 To c.Count     ' Remove name from the collection.
       c.Remove 1             ' Since collections are reindexed
                              ' automatically, remove the first
   Next                       ' member on each iteration.

   List1.Clear
   
   If Right(mypath, 1) <> "\" Then mypath = mypath & "\"

   filter = mypath & strExtension
   myname = Dir(filter)                                   ' Retrieve the first entry.

   Do While myname <> ""                                  ' Start the loop.
       DateModified = FileDateTime(mypath & myname)

       List1.AddItem DateModified & " " & myname
       'A different value for each itemdata  
       intN = intN + 1
       List1.ItemData(List1.NewIndex) = intN
       'List1.itemdata for this item must be the same as the Key associated with myname
       c.Add Item:=myname, Key:=CStr(intN)

       myname = Dir    ' Get next entry.
   Loop
   
List2.Clear
   
   For i = List1.ListCount - 1 To 0 Step -1
       List2.AddItem c.Item(CStr(List1.ItemData(i)))
   Next
   
End Function

Private Sub Command1_Click()
   GetDirToListBox "C:\", "*.txt"
End Sub

0
 

Author Comment

by:LarryZ
ID: 8197493
Hi DocM:

Thanks for trying so hard for me!

I can run your last code (3/24/2003 10:57 AM PST) with no run-time errors at all. Good so far. All (and only) files with the desired .01 extension read correctly into List1. Good so far.
Here they are, in the order they appear in List1, by the way:

<<<< List1>>>>>
3/14/2003 4:33:19 AM B1031101.01
3/15/2003 3:16:04 PM B1031501.01
3/17/2003 4:24:18 PM B1031701.01
10/7/2002 5:19:34 PM B1100701.01

(Notice that one of the files was from 2002)

Now here's the only problem left: files are still not sorted by DateModified in List2.
The code
  For i = List1.ListCount - 1 To 0 Step -1
      List2.AddItem c.Item(CStr(List1.ItemData(i)))
  Next
doesn't seem to work quite right. I'm familiar with the usage of the .AddItem and .ItemData properties for ListBoxes, but the Cstr has me baffled. Here's the order of List2 (remember I need oldest to newest date)

<<<< List 2 >>>>>>>
B1100701.01
B1031701.01
B1031501.01
B1031101.01

See what I mean about the order - it's just the inverse of whatever List1 reads in from the HDD directory.

I can see you've really put a lot of time into this already DocM. Sorry. I'll add 20 more points if it'll help to get List2 sorted chronologically.

Thanks a lot for your efforts.
0
 

Author Comment

by:LarryZ
ID: 8197772
List1 sorted nicely by Date with code from DocM after I remembered to set the ListBox's sorted property to True.
Thanks for all your hard earned help! Thanks also to HMadhur, though I never got a chance to follow up the initial concept offered.  Thanks Experts!
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

764 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