Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
673 views
in Technique[技术] by (71.8m points)

vbscript - Loop through ALL files in a folder based on 'Last Modified Date'

I need to loop through the files in a given folder in descending order of 'Last Modified Date'.

In the first iteration of the loop I need to be able to open the most recently modified file for reading and close it. In the second iteration, I need to be able to open the 2nd most recently updated file for reading and close it etc.

  1. Is there a built in method that allows a FileSystemObject to sort the files or do we absolutely have to write custom sorting routine?

  2. If we have to go with a custom sorting routine, is it possible to write this without having multiple functions? i.e. all code in the a main function.

  3. Speed is a concern since there are to be a lot of files to sort through. Therefore any custom procedures should be efficient.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could read the file names and dates into a disconnected recordset and sort that by date:

Set fso = CreateObject("Scripting.FileSystemObject")

Set list = CreateObject("ADOR.Recordset")
list.Fields.Append "name", 200, 255
list.Fields.Append "date", 7
list.Open

For Each f In fso.GetFolder("C:somewhere").Files
  list.AddNew
  list("name").Value = f.Path
  list("date").Value = f.DateLastModified
  list.Update
Next

list.MoveFirst
Do Until list.EOF
  WScript.Echo list("date").Value & vbTab & list("name").Value
  list.MoveNext
Loop

list.Sort = "date DESC"

list.MoveFirst
Do Until list.EOF
  WScript.Echo list("date").Value & vbTab & list("name").Value
  list.MoveNext
Loop

list.Close

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...