Order of Files collection in FileSystemObject

shwartz picture shwartz · Jun 3, 2013 · Viewed 25.4k times · Source

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).

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?

Answer

Papasmile picture Papasmile · Jun 4, 2013

Is it really too much code to sort?

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