根据“上次修改日期"遍历文件夹中的所有文件 [英] Loop through ALL files in a folder based on 'Last Modified Date'

查看:48
本文介绍了根据“上次修改日期"遍历文件夹中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按上次修改日期"的降序遍历给定文件夹中的文件.

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. 是否有内置方法允许 FileSystemObject 对文件进行排序,或者我们绝对必须编写自定义排序例程?

  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?

如果我们必须使用自定义排序例程,是否可以在没有多个函数的情况下编写它?即 a main 函数中的所有代码.

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.

速度是一个问题,因为有很多文件需要整理.因此,任何自定义程序都应该是有效的.

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

推荐答案

您可以将文件名和日期读入断开连接的 记录集 并按日期排序:

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

这篇关于根据“上次修改日期"遍历文件夹中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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