使用 VBScript 在单个文件夹中查找最近的文件日期 [英] Using VBScript to find most recent file date in a single folder

查看:21
本文介绍了使用 VBScript 在单个文件夹中查找最近的文件日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修改此 VBScript 以仅返回最新文件的名称和上次修改日期?目前它返回过去 24 小时内修改的任何内容.我只想查找最新的文件.我从 StackOverflow 借来的,还不是 VBScript 向导.

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:	est"  
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 

推荐答案

对于在 VBScript 上处理文件,建议您使用 FileSystemObject.

For working with files on VBScript it is recommended you use the FileSystemObject.

FileSystemObject 具有以下功能可帮助您解决问题:

The FileSystemObject has the following feature's that help you solve your problem:

  • FileSystemObject.GetFolder - 返回与指定路径中的文件夹对应的 Folder 对象.
  • Folder.Files - 返回由指定文件夹中包含的所有 File 对象组成的 Files 集合.
  • File.DateLastModified - 返回上次修改指定文件或文件夹的日期和时间.
  • 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.

为了演示,我提供了一个 GetRecentFile(和 GetRecentFolder)的实现,它扫描提供的输入路径并通过查找具有最大的 DateLastModified 属性.

To demonstrate, I have supplied an implementation of GetRecentFile (and GetRecentFolder) which scans the supplied input path and determines the most recent file by finding the file that has the greatest DateLastModified property.

Option Explicit  

Function GetRecentFile(path)
  Dim fso, file
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set GetRecentFile = Nothing
  For Each file in fso.GetFolder(path).Files
    If GetRecentFile is Nothing Then
      Set GetRecentFile = file
    ElseIf file.DateLastModified > GetRecentFile.DateLastModified Then
      Set GetRecentFile = file
    End If
  Next
End Function

Function GetRecentFolder(path)
  Dim fso, folder
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set GetRecentFolder = Nothing
  For Each folder in fso.GetFolder(path).SubFolders
    If GetRecentFolder is Nothing Then
      Set GetRecentFolder = folder
    ElseIf folder.DateLastModified > GetRecentFolder.DateLastModified Then
      Set GetRecentFolder = folder
    End If
  Next
End Function

Dim recentFile
Set recentFile = GetRecentFolder("C:Temp")
If recentFile is Nothing Then
  WScript.Echo "No recent files found"
Else
  WScript.Echo "Recent file is " & recentFile.Name & " " & recentFile.DateLastModified
End If

参考文献:

这篇关于使用 VBScript 在单个文件夹中查找最近的文件日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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