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

Order of filenames returned by DIR() 1

Status
Not open for further replies.

waynea

Programmer
Jul 11, 2000
41
US
I'm using the following code to read filenames from a directory into a semi-colon delimited string, which I need to have in either date or name order. However, it seems that this method returns the filenames in some other order (I'm guessing, according to the file's physical location in the directory ???) Does anyone know how to return these filenames in a specific order? I haven't been able to find a reference to this in any of my books.

' ----------------------------------------------------------Public Sub CheckDirectory(mstrDirectory As String)
Dim Counter As Long, strFname As String, mstrFileList As String

Counter = 0
mstrFileList = ""

' specify only .jpg files to be read
strFname = Dir(mstrDirectory & "*.jpg")

' Step through directory
While Not strFname = ""
If strFname <> &quot;.&quot; And strFname <> &quot;..&quot; Then
mstrFileList = mstrFileList & Chr(34) & mstrDirectory & strFname & Chr(34) & &quot;;&quot;
End If
' Read next filename
strFname = Dir
Wend

[sig][/sig]
 
DIR() does not return file names in any particular order. You will need to build an array of the returned file names and sort them from there. When finished, DIR() returns an empty string. [sig][/sig]
 

Try using the FileSystemObject, it gives the list in &quot;Name&quot; order. Copy this code to a form's Load event and put a breakpoint on the last line of code. First, make a Reference to &quot;Microsoft Scripting Runtime&quot; SCRRUN.DLL

Dim fso As FileSystemObject
Dim fld As Folder
Dim f As File

Set fso = New FileSystemObject
Set fld = fso.GetFolder(&quot;c:\&quot;)
For Each f In fld.Files
Debug.Print f.Name
Next
Set fld = Nothing
Set fso = Nothing
Unload Me

I hope this helps! [sig][/sig]
 
waynea,

I am almost certain that there is no direct way to have the &quot;DIR&quot; function return the contents in a specified order.

Whenever I want to accomplish this (type of) customization, I place the raw results into a UDT array which is dynamically dimensioned as results are added. Then, I sort the UDT according to the desired field(s) direction ... and use the sorted results.

I have taken the liberty of modifying the code you posted to do ~~~~ what you asked. I also modified it to be able to &quot;get&quot; different fitle &quot;types&quot; from a specified directory - mostly to make it easier for me to test.


Public Function basGetDir(MstrDir As String, FilTyp As String)

Dim Counter As Long
Dim strFname As String
Dim mstrFileList() As String 'Array of Values
Dim NotSorted As Boolean 'Flag to say the list is ordered
Dim OutList As String 'What to output to the text file
Dim Quo As String * 1 'Just shorthand for Chr(34)
Dim Idx As Integer
Dim Jdx As Integer
Dim MyFil As Integer


'Here we def the temp element to be able to swap two array elements
Dim ItemTemp As String 'Temp to hold first while we move second

Counter = 0
Quo = Chr(34)
ReDim mstrFileList(0) 'Initalize array to have the first element defined
' mstrFileList(Counter) = &quot;&quot;

' specify only .jpg files to be read
strFname = Dir(&quot;C:\&quot; & MstrDir & &quot;\&quot; & &quot;*.&quot; & FilTyp)

' Step through directory
While Not strFname = &quot;&quot;
If (strFname <> &quot;.&quot; And strFname <> &quot;..&quot;) Then
' mstrFileList(Counter) = mstrFileList & Chr(34) & mstrDirectory & strFname & Chr(34) & &quot;;&quot;
mstrFileList(Counter) = strFname 'Accumulate the File Names here
Counter = Counter + 1
ReDim Preserve mstrFileList(Counter)
End If
' Read next filename
strFname = Dir
Wend

ReDim Preserve mstrFileList(Counter - 1) 'Adjust the array to remove the last (Unused) element

'Now to sort this
NotSorted = True 'Assume it is sorted
While NotSorted
For Idx = 0 To UBound(mstrFileList) - 1
For Jdx = Idx + 1 To UBound(mstrFileList)
NotSorted = False 'Assume it is sorted
If (mstrFileList(Idx) < mstrFileList(Jdx)) Then
NotSorted = True 'No, It is Not Sorted
ItemTemp = mstrFileList(Idx)
mstrFileList(Idx) = mstrFileList(Jdx)
mstrFileList(Jdx) = ItemTemp
End If
Next Jdx
Next Idx
Wend

For Idx = 0 To UBound(mstrFileList)
OutList = OutList & Quo & mstrFileList(Idx) & Quo & &quot;;&quot;
Next Idx

'Now, we need to write the delimited list to a text file for use by others?
MyFil = FreeFile
Open &quot;DelimFileNames.Txt&quot; For Output As #FreeFile
Print #MyFil, OutList
Close #MyFil


End Function


I did not add any error checking, however it (error checking) should be included - especially for the inital DIR call, because if this does not find a file, the function will fail on the redim where we reduce the number of elements in the array.

p.s. sorry this took so long, I just saw the Item Monday PM and was out Tue.

[sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
Thanks to all for the replies - gives me several options to work with. VB400 - appreciate that tip. Since I already have a reference open to the FileSystemObject, that ought to be exactly what I need for this particular project. [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top