使用 VBScript 在单个文件夹中查找最新文件日期

发布于 2025-01-04 00:58:25 字数 569 浏览 4 评论 0原文

我如何修改此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光暖心i 2025-01-11 00:58:25

要在 VBScript 上处理文件,建议您使用 FileSystemObject

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

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

为了演示,我提供了 GetRecentFile (和 GetRecentFolder)的实现,它扫描提供的输入路径并通过查找具有最大 的文件来确定最新文件>DateLastModified 属性。

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

参考文献:

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

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 (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

References:

半世蒼涼 2025-01-11 00:58:25

这对我有用!之前答案的一个非常破碎的版本。

Set GetRecentFile = Nothing
For Each objFile In objFolder.Files
    If GetRecentFile is Nothing then
        Set GetRecentFile = objFile
    ElseIf objFile.DateLastModified > GetRecentFile.DateLastModified then
        Set GetRecentFile = objFile
    End If
Next

This worked for me! A very much broken down version of previous answer.

Set GetRecentFile = Nothing
For Each objFile In objFolder.Files
    If GetRecentFile is Nothing then
        Set GetRecentFile = objFile
    ElseIf objFile.DateLastModified > GetRecentFile.DateLastModified then
        Set GetRecentFile = objFile
    End If
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文