shw*_*rtz 4 vbscript filesystemobject
在VBScript中,我想获取按创建日期排序的文件夹中的文件列表.我看到为了做到这一点,我需要使用一个记录集(对我来说似乎有点过分)或者自己对集合进行排序(我想我可以避免它,我希望我的代码更短).
因为我是创建文件的人,所以我使用以日期开头的名称(yyyy_mm_dd)创建它们,所以尽管如此我如果能够获得至少按名称排序的文件,那么我就完全了.不幸的是,FileSystemObject的Files集合的MSDN文档没有说明集合的顺序.有没有人知道其他一些秘密文件或类似的东西可以更具体?
小智 9
排序的代码真的太多了吗?
set fso = CreateObject("Scripting.FileSystemObject")
Set outputLines = CreateObject("System.Collections.ArrayList")
for each f in fso.GetFolder(".").files
outputLines.Add f.Name
next
outputLines.Sort() ' 5 lines...
For Each outputLine in outputLines
set file = fso.GetFolder(".").files.item (outputLine&"")
Wscript.Echo file.name ' TODO: your thing here
Next
Run Code Online (Sandbox Code Playgroud)
如果您想按特定顺序获取文件夹中的文件,则必须自己完成.如果您不喜欢ADO记录集或使用可排序的.NET集合,您可以shell(.Run,.Exec)并处理输出dir /A:-D /B /O:D /T:C(无文件夹,裸格式(无标题/摘要),order:date, timefield:创作).
更新:
虽然我确实可以展示.Files集合按名称命令按顺序排列元素的例子,盖茨先生明确表示:
INFO:FileSystemObject的限制...无法对文件集合中的文件名进行排序 - 您可以遍历Files集合中的File对象以获取文件夹中文件的列表.但是,File对象未排序.您需要使用排序例程来对Files集合中的File对象进行排序.
简单的演示代码,显示:如果要使用shell功能,则需要shell(%comspec%) - 如内部命令:
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
Run Code Online (Sandbox Code Playgroud)
输出:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22116 次 |
| 最近记录: |