vba VBA中文件夹中文件名的排序列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2811558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:38:00  来源:igfitidea点击:

Sorted list of file names in a folder in VBA?

filevba

提问by Karsten W.

Is there a way to get a sorted list of file names of a folder in VBA? Up to now, I arrived at

有没有办法在 VBA 中获取文件夹的文件名排序列表?到目前为止,我到达了

Dim fso As Object
Dim objFolder As Object
Dim objFileList As Object
Dim vFile As Variant
Dim sFolder As String

sFolder = "C:\Docs"

Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(sFolder)
Set objFileList = objFolder.Files

For Each vFile In objFileList
    ' do something '
Next vFile

but it is crucial to be sure the processing order of the for loop is determined by the file names...

但确保 for 循环的处理顺序由文件名决定至关重要...

Any help appreciated!

任何帮助表示赞赏!

回答by Mitch Wheat

Download a VBA sort routine such as this one: Recursive Quick Sort

下载 VBA 排序例程,例如:递归快速排序

Fill an array with the file names and simply sort as shown here.

用文件名填充一个数组,然后简单地按此处所示进行排序。

回答by dcp

Looks like you can do it by using an ADODB.RecordSet. It's a bit heavy duty, but here's a reference which should get you started.

看起来您可以通过使用ADODB.RecordSet. 这有点繁重,但这里有一个参考资料,可以帮助您入门。

回答by Alex K.

The order is arbitrary.

顺序是任意的。

When you walk over objFileListadd the files to an array, then sort the array.

当您遍历时,objFileList将文件添加到数组中,然后对数组进行排序