我正在寻找一个脚本,该脚本将比较最多四个文件的最后修改日期,然后捕获最近修改的文件。我当前的障碍是某些文件可能不存在。
如果文件不存在,则设置将失败,但是我可以通过查找文件来轻松克服该问题,如果文件不存在,则只需跳过set命令即可。这将导致创建数组失败,因为我的变量现在为空。 有什么建议可以解决这个问题吗?
这是我到目前为止所拥有的:
Option Explicit
Dim objFSO, path, file, recentDate, recentFile, File1, File2, File3, File4, afiles, File1date, date1
Set objFSO = CreateObject("Scripting.FileSystemObject")
set File1=objFSO.getfile("c:\temp\file.txt")
set File2=objFSO.getfile("c:\test\File.txt")
set File3=objFSO.getfile("c:\users\%profile%\documents\File.txt")
set File4=objFSO.getfile("c:\users\public\documents\File.txt")
'Prepare variables to store the required information
Dim dateMin, dateMax
date1 = File1.datelastmodified
wscript.echo date1
wscript.echo now
dateMin = date1
dateMax = date1
afiles = Array( File1.datelastmodified, File2.datelastmodified, File3.datelastmodified, File4.datelastmodified )
Dim i
For i=1 to UBound(aFiles)
If aFiles(i) < dateMin Then dateMin = aFiles(i)
if aFiles(i) > dateMax Then dateMax = aFiles(i)
Next
'
' Output Information
WScript.Echo "Highest: " & CStr( dateMax )
WScript.Echo " Lowest: " & CStr( dateMin )
答案 0 :(得分:0)
我认为,如果您使用ArrayList
捕获每个文件的DateLastModified
,这会容易得多。
与VBScript数组不同,使用ArrayList可以动态添加值。
此外,ArrayList有一个非常有用的方法,称为Sort()
,我们可以在这里使用它来确定dateMin
和dateMax
的值。
尝试:
Option Explicit
Dim objFSO, objList, objFile, arrFiles, fileName
arrFiles = Array("c:\temp\file.txt","c:\test\File.txt","c:\users\%profile%\documents\File.txt","c:\users\public\documents\File.txt")
Set objList = CreateObject("System.Collections.ArrayList")
Set objFSO = CreateObject("Scripting.FilesystemObject")
For Each fileName In arrFiles
If objFSO.FileExists(fileName) Then
Set objFile = objFSO.GetFile(fileName)
objList.Add objFile.DateLastModified
End If
Next
objList.Sort()
' Prepare variables to store the required information
Dim dateMin, dateMax
dateMin = objList.item(0)
dateMax = objList.item(objList.Count - 1)
' Output Information
WScript.Echo "Highest: " & CStr( dateMax )
WScript.Echo "Lowest: " & CStr( dateMin )
' Clean up
Set objFSO = Nothing
Set objFile = Nothing
Set objList = Nothing
修改
上面的代码是对原始代码的翻版。可能我误会了,您的目标是找到最近修改的文件。
这应该可以实现您的想法:
Option Explicit
Dim objFSO, objFile, objLatest, fileName, arrFiles
arrFiles = Array("c:\temp\file.txt","c:\test\File.txt","c:\users\%profile%\documents\File.txt","c:\users\public\documents\File.txt")
Set objFSO = CreateObject("Scripting.FilesystemObject")
Set objLatest = Nothing
For Each fileName In arrFiles
If objFSO.FileExists(fileName) Then
Set objFile = objFSO.GetFile(fileName)
If (objLatest Is Nothing) Then
Set objLatest = objFile
ElseIf (objFile.DateLastModified > objLatest.DateLastModified) Then
Set objLatest = objFile
End If
End If
Next
If objLatest Is Nothing Then
WScript.Echo "None of the files in 'arrFiles' exist.."
Else
' Here you decide if you want to keep the latest file as Object (-> objLatest) or just as a string to the full pathname of that file
Dim latestFile
latestFile = objFSO.GetAbsolutePathName(objLatest) ' store the full path and filename
' Info on FormatDateTime() at https://www.w3schools.com/asp/func_formatdatetime.asp
WScript.Echo "Most recently modified file is '" & latestFile & "' at " & FormatDateTime(objLatest.DateLastModified, 0)
End If
' Clean up
Set objFSO = Nothing
Set objFile = Nothing
Set objLatest = Nothing