PDA

Click to See Complete Forum and Search --> : Using the DIR command


seaweed
Jan 28th, 2000, 07:18 AM
Well, this is a lengthy answer, but basically what I would do is use a QuickSort routine to sort the array of file names after you obtain them.

Following is the code for the QuickSort (which you would put in a module), and a routine to get the file names (which I didn't test, so I can't guarantee it). I know the QuickSort works great, though, becuase I use it all the time.

By the way, I just copied and pasted this from my VB window, so the formatting may be a little off. Try cutting and pasting it into your VB window and see if it comes out alright. Ok, Here goes...

'***************************************************************************
'Put the following function into a module:
'***************************************************************************

Public Sub QuickSort(SortList As Variant, ByVal First As Integer, _
ByVal Last As Integer)
'
'Purpose: This function sorts a list from lowest to highest (0 - 9 for
' numeric arrays, A - z for string arrays). This is a recursive
' function, meaning that it calls itself in order to solve
' the problem.
'
'Arguments: SortList: An array of items to be sorted (Variant)
' First: The index of the first item in the array (Integer)
' Last: The index of the last item in the array (Integer)
'
'Note: There is no "return value". However, the argument list is
' returned with it's elements sorted. If you want to retain
' your original list, pass this function a copy of the list.
'
Dim Low As Integer, _
High As Integer, _
Temp As Variant, _
TestElement As Variant

Low = First
High = Last
TestElement = SortList((First + Last) / 2)

Do
Do While SortList(Low) < TestElement
Low = Low + 1
Loop

Do While SortList(High) > TestElement
High = High - 1
Loop

If (Low <= High) Then
Temp = SortList(Low)
SortList(Low) = SortList(High)
SortList(High) = Temp
Low = Low + 1
High = High - 1
End If
Loop While (Low <= High)

If (First < High) Then
Call QuickSort(SortList, First, High)
End If

If (Low < Last) Then
Call QuickSort(SortList, Low, Last)
End If

End Sub

'***************************************************************************
'Put the following code into the procedure that gets the file names:
'***************************************************************************

'Create a string array to hold file names
Dim FileName() As String, _
nCounter As Integer

nCounter = 1

'Enter first file name into array position 1
ReDim FileName(nCounter)
FileName(nCounter) = Dir("*.dat")

'If there was a file there, continue to enter the file
'names into the array (in any order)
Do While FileName(nCounter) <> ""
nCounter = nCounter + 1
ReDim Preserve FileName(nCounter)
FileName(nCounter) = Dir
Loop

'Remove the last item (empty string)
ReDim FileName(nCounter - 1)

'Use the QuickSort to sort the filenames alphabetically
Call QuickSort(FileName, 1, nCounter - 1)

'Now you have an array of file names sorted alphabetically!!

seaweed
Jan 28th, 2000, 07:28 AM
Funny how you don't notice your errors until AFTER you post...

I'm pretty sure that the last Redim should be Redim Preserve, and you will probably need to make sure that you have at least two file names in your array before calling the QuickSort function (you wouldn't need to sort with only one item)!

Good Luck.

ndr01
Jan 28th, 2000, 11:30 AM
I am currently using the DIR command to access files.

filename = DIR("*.dat")

I need these files to be returned in alphabetical order which does not seem to be the way that DIR works.

Is there another command to do this?

Any help would be greatly received.

Thanks

An extremely inexperienced VB guy