• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1430
  • Last Modified:

Visual Basic 6 File List Boxes time and date sorting

In VB6 filelistboxes sort the files alphabetically I need to be able to sort them by date and time created.
Preferrably with the oldest file at the top of the list and the newest file at the bottom. It needs to be quick and I do not want to use databases.
  • 3
  • 2
1 Solution
You will need to replace the filelistbox with a standard listbox and list the files inside a directory by yourself. Scan thru the directory for files, get the filesizes, sort, add to list. poc. The point is that don't get stuck with the filelistbox, use a listbox instead. All the neccessary code samples can be found at www.planet-source-code.com.
Depending on what you are doing, you could try the browser control instead of the file list box...

'using the browser dialog box
'make a reference

Option Explicit

Private Const MAX_PATH = 260

Private Declare Function SHBrowseForFolder Lib _
"shell32" (lpbi As BrowseInfo) As Long

Private Declare Function SHGetPathFromIDList Lib _
"shell32" (ByVal pidList As Long, ByVal lpBuffer _
As String) As Long

Private Declare Function lstrcat Lib "kernel32" _
Alias "lstrcatA" (ByVal lpString1 As String, ByVal _
lpString2 As String) As Long

Private Type BrowseInfo
   hWndOwner As Long
   pIDLRoot As Long
   pszDisplayName As Long
   lpszTitle As Long
   ulFlags As Long
   lpfnCallback As Long
   lParam As Long
   iImage As Long
End Type

Private Sub Form_Load()
'Opens a Browse Folders Dialog Box that displays the
'directories in your computer
Dim lpIDList As Long ' Declare Varibles
Dim sBuffer As String
Dim szTitle As String
Dim tBrowseInfo As BrowseInfo

szTitle = "Hello World. Click on a directory and " & _
"it's path will be displayed in a message box"
' Text to appear in the the gray area under the title bar
' telling you what to do

With tBrowseInfo
   .hWndOwner = Me.hWnd ' Owner Form
   .lpszTitle = lstrcat(szTitle, "")
End With

lpIDList = SHBrowseForFolder(tBrowseInfo)

If (lpIDList) Then
   sBuffer = Space(MAX_PATH)
   SHGetPathFromIDList lpIDList, sBuffer
   sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
'          <<< Add Your Code Here >>>
                 MsgBox sBuffer
    End If
End Sub

Forget Filelistbox. Use a Listview (in the Common Controls OCX). This is what is used in thw Windows Explorer (the pane on the right).
To fill the listview with data you can/should use the FileSystemObject. To use this you have to add a reference to Microsoft Scripting Runtime (SCRRUN.DLL). Once you've added this reference add the following lines in your code to use the FileSystemObject:

dim fsoMain as FileSystemObject
Set fsoMain = new FileSystemObject

The FileSystemObject contains datatypes like Drive, File, Folder, Subfolder,...
It is incredibly easy to get information about paths, files, folders,...

You will need to program a little bit but it is realy fun to use because this is what is missing in standard VB.

When you need more info, send me a mail (nrgiser@bigfoot.com). I'll send you an example project.

BTW: With not more than 30 lines of code you can program your own light weight explorer with this baby.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

"You will need to program a little bit but it is realy fun to use because this is what is missing in standard VB. "

Nope, it's all there, you just have to use what's on top of your shoulders..
To VbMaster:

Have you ever used FileSystemObject ? If not, try it. You will see how much simpler it is.
Yes, I did try it once  when a 'expert' here said that his code using the FileSystemObject was faster than pure VB code, just to prove that he was wrong. ;)

A big dependency file, problems with version compatibiliy, bad performance.. I do not see ANY reason why to use it when everything it does can be implemented using a small amount of VB code.


Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now