使用 VBScript 在单个文件夹中查找最新文件日期
我如何修改此 VBScript 以仅返回最新文件的名称和上次修改日期?目前,它返回过去 24 小时内修改的所有内容。我只想查找最新的文件。我从 StackOverflow 借用了它,但还不是 VBScript 向导。
option explicit
dim fileSystem, folder, file
dim path
path = "C:\test"
Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set folder = fileSystem.GetFolder(path)
for each file in folder.Files
if file.DateLastModified > dateadd("h", -24, Now) then
'whatever you want to do to process'
WScript.Echo file.Name & " last modified at " & file.DateLastModified
end if
next
How could I modify this VBScript to return only the newest file's name and Last Modified date? Currently it returns anything modified in the last 24 hours. I want to look for the most recent file only. I borrowed this from StackOverflow, not yet a VBScript wizard.
option explicit
dim fileSystem, folder, file
dim path
path = "C:\test"
Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set folder = fileSystem.GetFolder(path)
for each file in folder.Files
if file.DateLastModified > dateadd("h", -24, Now) then
'whatever you want to do to process'
WScript.Echo file.Name & " last modified at " & file.DateLastModified
end if
next
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要在
VBScript
上处理文件,建议您使用FileSystemObject
。FileSystemObject
具有以下功能,可帮助您解决问题:FileSystemObject.GetFolder
- 返回与指定路径中的文件夹相对应的文件夹对象。Folder.Files
- 返回由指定文件夹中包含的所有 File 对象组成的 Files 集合。File.DateLastModified
- 返回上次修改指定文件或文件夹的日期和时间。为了演示,我提供了
GetRecentFile
(和GetRecentFolder
)的实现,它扫描提供的输入路径并通过查找具有最大的文件来确定最新文件>DateLastModified
属性。参考文献:
For working with files on
VBScript
it is recommended you use theFileSystemObject
.The
FileSystemObject
has the following feature's that help you solve your problem:FileSystemObject.GetFolder
- Returns a Folder object corresponding to the folder in a specified path.Folder.Files
- Returns a Files collection consisting of all File objects contained in the specified folder.File.DateLastModified
- Returns the date and time that the specified file or folder was last modified.To demonstrate, I have supplied an implementation of
GetRecentFile
(andGetRecentFolder
) which scans the supplied input path and determines the most recent file by finding the file that has the greatestDateLastModified
property.References:
这对我有用!之前答案的一个非常破碎的版本。
This worked for me! A very much broken down version of previous answer.