FileSystemObject 中的文件集合顺序 [英] Order of Files collection in FileSystemObject
问题描述
在 VBScript 中,我想获取按创建日期排序的文件夹中的文件列表.我看到为了做到这一点,我需要使用记录集(对我来说似乎有点矫枉过正)或自己对集合进行排序(我想我可以避免它并且我希望我的代码更短).
In VBScript, I want to get a list of files in a folder ordered by creation date. I saw that in order to do that I will need to either use a record set (seems like an overkill to me) or sort the collection myself (I think I can avoid it and I want my code to be shorter).
因为我是创建文件的人,所以我创建的文件名称以日期 (yyyy_mm_dd) 开头,所以我认为,如果我至少可以按名称获得文件,那么我就准备好了.不幸的是,来自 FileSystemObject 的 Files 集合的 MSDN 文档 没有说明集合的顺序.有谁知道其他一些秘密文件或类似的更具体的东西?
Since I am the one creating the files, I create them with names that begin with the date (yyyy_mm_dd) so I though that if I can get the files at least ordered by name then I'm all set. Unfortunately, the MSDN documentation of the Files collection from FileSystemObject doesn't say anything about the order of the collection. Does anyone know of some other secret documentation or something like that that can be more specific?
推荐答案
如果您想按特定顺序获取文件夹中的文件,则必须自己完成.如果您不喜欢 ADO 记录集或使用可排序的 .NET 集合,您可以退出 (.Run, .Exec) 并处理 dir/A:-D/B/O:D/T 的输出:C
(无文件夹,裸格式(无标题/摘要),订单:日期,时间字段:创建).
If you want to get the files in a folder in a specific order, you'll have to do it yourself. If you don't like the ADO recordset or using a sortable .NET collection, you can shell out (.Run, .Exec) and process the output of dir /A:-D /B /O:D /T:C
(no folders, bare format (no header/summary), order:date, timefield:creation).
更新:
虽然我确实可以展示 .Files 集合按名称顺序传递其元素的示例,但盖茨先生明确地说:
While I surely can show examples where the .Files collection delivered its elements ordered by name, Mr. Gates explicitly says:
信息:FileSystemObject 的限制...无法对文件名进行排序来自文件集合 - 您可以遍历 File 对象在 Files 集合中获取文件夹中文件的列表.但是,File 对象未排序.你需要使用一个排序对 Files 集合中的 File 对象进行排序的例程.
INFO: Limitations of the FileSystemObject ... Cannot sort file names from the files collection - you can iterate through the File objects in the Files collection to obtain a list of the files in a folder. However, the File objects are not sorted. You need to use a sort routine to sort the File objects in the Files collection.
显示的简约演示代码:如果您想使用 shell 功能,您需要一个 shell (%comspec%) - 例如 内部命令:
Minimalistic demo code that shows: You need a shell (%comspec%) if you want to use shell features - like intrinsic commands:
Option Explicit
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim goWS : Set goWS = CreateObject("WScript.Shell")
Dim csDir : csDir = "c: emp"
WScript.Quit demoSF()
Function demoSF()
demoSF = 0
Dim aDSOrd : aDSOrd = getDSOrd(csDir, "%comspec% /c dir /A:-D /B /O:D /T:C """ & csDir & """")
Dim oFile
For Each oFile In aDSOrd
WScript.Echo oFile.DateCreated, oFile.Name
Next
End Function ' demoSF
Function getDSOrd(sDir, sCmd)
Dim dicTmp : Set dicTmp = CreateObject("Scripting.Dictionary")
Dim oExec : Set oExec = goWS.Exec(sCmd)
Do Until oExec.Stdout.AtEndOfStream
dicTmp(goFS.GetFile(goFS.BuildPath(sDir, oExec.Stdout.ReadLine()))) = Empty
Loop
If Not oExec.Stderr.AtEndOfStream Then
WScript.Echo "Error:", oExec.Stderr.ReadAll()
End If
getDSOrd = dicTmp.Keys()
End Function
输出:
cscript 16895525.vbs
07.10.1998 15:31:34 TlbInf32.chm
..
09.10.2008 22:40:29 sqlce.sql
09.10.2008 22:40:29 gltsqlcopytest.sdf
05.11.2008 20:11:39 Vorfuehrung.class
..
28.03.2011 20:23:36 Program.cs
.
01.10.2012 10:10:10 KyXHDe.chm
这篇关于FileSystemObject 中的文件集合顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!