FileSystemObject中文件收集的顺序 [英] Order of Files collection in FileSystemObject

查看:75
本文介绍了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中文件集合的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:

INFO:FileSystemObject的限制...无法对文件名进行排序 从文件集合中获取-您可以遍历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.

显示以下内容的简约演示代码:如果要使用外壳功能,则需要外壳(%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:\temp"

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆