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

Listing Files from a folder - in alpha order

Status
Not open for further replies.

Maturi

IS-IT--Management
Oct 9, 2003
81
GB
Hi,

I trying to list the files in a folder and show the output list in a Combo Box (CB_List) using the code

Code:
Dim fso As New FileSystemObject
Dim f As File
Dim fld As Folder
Dim FilesinFolder As Files
Dim MyPath As String

MyPath = "c:\docs"

Set fld = fso.GetFolder(MyPath)

Set FilesinFolder = fld.Files

For Each f In FilesinFolder
	Pack = Pack & ";" & f.Name
Next
    Me!CB_List.RowSource = Pack

The files in 'FilesinFolder' appear to be in a random order (when I list them out in the combo box).

Is there a way to order them by name/alpha order either before doing Me!CB_List.RowSource = Pack

or after it's in the combo box??

Thanks

M
 
Bob

Thanks - but I'm getting the message...

"Object does not support this property or method" for the '.Sorted =True'

Is there a DLL/Reference I need to include? Or is it something else.

Apreciate your help

Mat
 
Maturi,
Are you sure you are using it in VB6? I think it is AccessVBA

________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 


You are right - It is Access VBA

Wrong Forum.

Can you still help?
 
Why not you are using the modified function bby TheAceman1 in thread702-1158417? It is sorted by alpha
Code:
    Dim sPath As String, sFile As String, Pack As String
    sPath = "c:\docs\"
    sFile = Dir(sPath, vbDirectory)
    Me.CB_List.RowSourceType = "Value List"

    Do While sFile <> ""
        If sFile <> "." And sFile <> ".." Then
            If Pack <> "" Then
                Pack = Pack & ";" & sFile
            Else
                Pack = sFile
            End If
        End If

        sFile = Dir
    Loop

    Me!CB_List.RowSource = Pack

________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 
Zameer,

Tried that approach - yes it does do a similar job but it still does not order the files in name order. It seems to depend on the folder you open.

Any other suggestions?

Other wise I will post the query in the Access VBA forum

 
But it is giving a sorted result for me.

Anyway if you want to post in Access VBA forum then post it. don't remember to link both threads in to it.

________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 
I mean don't forget to link..[blush]

________________________________________________________
Zameer Abdulla
Help to find Missing people
Do not cut down the tree that gives you shade.
 
You could just use the old DOS sort pipe:
Code:
Shell "cmd /c dir | sort /+39 /O temp.txt"
will put a sorted list of the directory in a file called temp.txt. Of course, the folder names will be sorted into the list as well, so you'll have to remove them in your routine that populates your combo box.

HTH

Bob
 
Bob,

That's a pretty good idea. Expanding on that, I came up with this...

Code:
    Dim FSO As Scripting.FileSystemObject
    Dim cFolder As String
    Dim cFiles As String
    Dim arFiles() As String
    Dim i As Long
    
    cFolder = "C:\"
    
    Shell "cmd /c Dir """ & cFolder & "*.*"" /b /o /a:-d > """ & App.Path & "\Files.txt"""
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    cFiles = FSO.OpenTextFile(App.Path & "\Files.txt", ForReading).ReadAll
    Set FSO = Nothing
    
    arFiles = Split(cFiles, vbCrLf)
    For i = LBound(arFiles) To UBound(arFiles)
        Debug.Print arFiles(i)
    Next

Check out all those command line arguments to the dir command.

/b removes the header stuff
/o sorts the list alphabetically
/a:-d removes directories from the listing

The original poster wanted the list in a combo box, so instead of debug.print you would need to add the items to the combo box.

Hope this helps.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Well, that got me going. I typed "help dir" on the command line. It would seem that dir has grown up since the old DOS 3.x days. George, I played around a bit and I have a couple of observations: *.* isn't necessary (might still be a good idea, of course), since dir does that anyway, and /b also sorts. So, one could get away with
Code:
Shell "cmd /c Dir """ & cFolder & """ /b /a-d > """ & App.Path & "\Files.txt"""
if one had a bent for simplification over specification. In any case, the dir switches are a definite improvement over using the sort pipe.

On the other hand, the sort pipe isn't obsolete: given that you can sort by any column you like, you can do things you can't do with dir switches. For example,
Code:
shell "cmd /c dir \myfolder | sort /+22"
will sort from smallest file to largest, with all directories sorted to the bottom.

Bob
 
Here's a WSH script, easily converted to VB:

sortdir.wsf
Code:
<job>
  <object id="FSO" progid="Scripting.FileSystemObject"/>
  <object id="SORTER" progid="ADODB.Recordset"/>
  <reference object="ADODB.Recordset"/>
  <script language="VBScript">
    Option Explicit

    Dim f, str

    With SORTER
      .Cursorlocation = adUseClient
      .Fields.Append "FileName", adVarWChar, 255
      .Open
      .Sort = "FileName ASC"

      For Each f In FSO.GetFolder("C:\").Files
        .AddNew
        .Fields("FileName").Value = f.Name
      Next

      str = ""
      .MoveFirst
      Do While Not .EOF
        str = str & .Fields("Filename").Value & vbNewLine
        .MoveNext
      Loop
      .Close
      MsgBox str
    End With
  </script>
</job>
Standalone ADO Recordsets are lighter weight than one might think. I've even used them as the workspace in line-oriented embedded text editors that only had to handle 2000 lines or so.
 
dilettante,

That's another good idea. Maybe we can get Bob to do some benchmark testing. [wink]

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Something like the following should also work:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Path = '\\file downloads\\'") ' order by name ascending")

For Each objFile In colFiles
Debug.Print objFile.FileName
Next
 
Bob, I've dropped another WMI solution into thread222-1159645 ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top