Process files using FSO - in alphabetical order

Formula11

Active Member
Joined
Mar 1, 2005
Messages
433
Office Version
  1. 365
Platform
  1. Windows
If FSO is used to process files, can you make it do so in alphabetical order.
If I run this code, normally it does in alphabetical order, but if I amend one of the files, then it changes order. It's as if both alphabvetical order and last saved or last modified is used.

VBA Code:
Sub Add_files_to_menu(cb As CommandBar, blnBeginGroup As Boolean)
    Dim FSO As Object, SourceFolder As Object, FileItem As Object
    Dim m As CommandBarPopup
    Dim mi As CommandBarButton
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set SourceFolder = FSO.GetFolder("C:\Files\")
    Set m = cb.Controls.Add(msoControlPopup, , , , True): With m: .BeginGroup = blnBeginGroup: .Caption = "List files": End With
        For Each FileItem In SourceFolder.Files
            Set mi = m.Controls.Add(msoControlButton, , , , True)
            With mi
                .Caption = FileItem
            End With
        Next FileItem
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You can sort if you load the items into an array first.
To sort immediately you can use the Wscript shell object.
 
Upvote 0
This code puts all xls files of a folder in one array(sorted in alphabetical order, because of "o:n" in the code below)
For more info: dir

After this you can loop through all items in ar.

VBA Code:
Sub jec()
ar = Split(CreateObject("wscript.shell").exec("cmd /c Dir ""C:\Files\*.xls"" /b/o:n").stdout.readall, vbCrLf)
End Sub

If you want all items, including subolders, you should use this: /b/o:n/s
 
Upvote 0
Thanks again.
Do you think you can make it work with the original query.

So far I tried to simplify to just list and this sort of worked. Each file was listed but there was a blank entry too.
When I tried to list in menu it was all blank.

VBA Code:
Sub Add_files_to_menu()
    Dim FSO As Object, SourceFolder As Object, FileItem As Object
    
    Dim ar As Variant
    Dim item As Variant
    ar = Split(CreateObject("wscript.shell").exec("cmd /c Dir ""C:\Files\"" /b/o:n").StdOut.ReadAll, vbCrLf)
    
    For Each item In ar
    MsgBox item
    Next
    
'    Set FSO = CreateObject("Scripting.FileSystemObject")
'    Set SourceFolder = FSO.GetFolder("C:\Files\")
'        For Each FileItem In SourceFolder.Files
'            MsgBox FileItem
'        Next FileItem
End Sub
 
Upvote 0
To me it's not very clear what you are trying to reach. I also think you posted just 1 piece of your code.
 
Upvote 0
I'm trying to list files in a menu, when clicked on, it will open them. I've left out this part to simplify. Normally FileSystemObject shows in alphabetical order but for some reason or other, the order changes, maybe when I edit.
FSO fundamentally does not order files.
 
Upvote 0
In order to directly get files by ascending order just use VBA Dir function rather than FSO …​
 
Upvote 0
Post #4 does that as well
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top