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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Files from Newest to Oldest with Dir

Status
Not open for further replies.

dlpastel

Programmer
Aug 8, 2002
114
US
I need to sort the filenames in a directory from newest to
oldest with the Dir command. I know the Dir command does not include this feature and I believe you need to use a array. Could someone show me how to do this?

I want to sort the following filename from newest to oldest

C:\Apc\Actv\*.VNM

Thanks,
Dan
 
If DIR command does not include this feature then why will you want to use it?
 
Expanding on vladk's suggestion....

Use the shell command to get a directory listing in date order, saved to a file. Dir C:\ /O:D > C:\DirList.txt

C:\ is the folder to check (replace this with your folder)
/O:D causes the list to be sorted in date order
> C:\DirList.txt causes the output to be redirected to a file

Open the C:\DirList.txt file to extract the sorted list.

Code:
    Dim iFile As Integer
    Dim strFile As String
    
    Shell "cmd /c Dir C:\ /O:D > C:\DirList.txt"
    
    iFile = FreeFile
    Open "C:\DirList.txt" For Input As #iFile
    
    While Not EOF(iFile)
        Line Input #iFile, strFile
        If IsDate(Left(strFile, 10)) Then
            ' Put an additional check for the extension you want
            List1.AddItem (Right(strFile, Len(strFile) - 39))
        End If
    Wend
    Close #iFile

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
The program is a interactive voicemail program and if I use a Access Database the system crashes when there are more than 70 simultaneous users. Access just cannot handle it. I could move to SQL Server but that requires extensive re-coding. I like the Dir command because it is a windows command and Windows 2000 works great for multitasking with it.

Dan
 
I tried the code above by gmmastros but the problem is that it takes too long to create the file and the rest of the code tries to open the file before it is created. That is why I think it would be best to use an array.

Dan
 
>I can't help thinking there must be a simpler w

Possibly - but disconnected recordsets are very, very useful and (IMHO) shamefully underused by VB programmers
 
strongm, I agree.

Ordinarily, I would have suggested using the File System Object to get the list of files, and then used a disconnected recordset to sort them. In this case, I didn't suggest it because I didn't know the original poster was already using ADO in his/her project. The method used by the link provided by petermeachem is how I would have done this in my own project.



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
If recordsets are not feasible in this situation, you can use the following API code which uses file searching APIs.

I used these APIs because they return the FileName and the FileTime in a single hit to the disk. Using VB, we have to use two separate functions for this purpose; Dir and FileDateTime which requires hitting the disk twice.

Not to mention that FSO also returns FileName and FileTime in a single call but it has more overhead then API and I prefer using API.
___
[tt]
Option Explicit
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Const MAX_PATH = 260
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Private Type FileInfo
FileName As String
Modified As Currency
End Type
Function DirEx(Path As String) As String()
Dim hFind As Long, wfd As WIN32_FIND_DATA, Files() As FileInfo, N As Long, I As Long
'create file list
hFind = FindFirstFile(Path, wfd)
Do
If (wfd.dwFileAttributes And vbDirectory) = 0 Then
ReDim Preserve Files(N)
Files(N).FileName = Left$(wfd.cFileName, lstrlen(wfd.cFileName))
CopyMemory Files(N).Modified, wfd.ftLastWriteTime, 8
N = N + 1
End If
Loop While FindNextFile(hFind, wfd)
FindClose hFind
'sort file list
Dim File As FileInfo
For N = 0 To UBound(Files)
For I = N + 1 To UBound(Files)
If Files(N).Modified > Files(I).Modified Then
File = Files(I)
Files(I) = Files(N)
Files(N) = File
End If
Next
Next
'return file list
ReDim V(UBound(Files)) As String
For N = 0 To UBound(Files)
V(N) = Files(N).FileName
Next
DirEx = V
End Function

Private Sub Form_Load()
Dim Files() As String, N As Long
Files = DirEx("C:\Apc\Actv\*.VNM")
For N = 0 To UBound(Files)
Debug.Print Files(N)
Next
End Sub[/tt]
___

George, their is a much easier way to get only filenames out of the DIR command.

[tt][ignore]Shell "cmd /c DIR C:\ /O:-D /B > C:\DirList.txt"[/ignore][/tt]

Note that [ignore]/O:-D[/ignore] reverses the sort order (Newest to Oldest) and /B, which specifies the bare format removes all information (like date and time) but the filename.
 
Thank You Hypertia, That was just what I was looking for. How would I go about looking up more information about these libs?

Thanks,
Dan
 
If you want to know more about Win32 API, please see thread222-504995.
You may also want to see these related forums: forum711 and forum713.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top