VBscript创建数组但省略空白变量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VBscript创建数组但省略空白变量相关的知识,希望对你有一定的参考价值。

我希望创建一个脚本,比较最多4个文件的最后修改日期,然后捕获最近修改的文件。我目前的路障是某些文件可能不存在。

如果文件不存在,该集将失败,但我可以通过查找文件轻松克服该问题,如果不存在则只需跳过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:	empfile.txt")
set File2=objFSO.getfile("c:	estFile.txt")
set File3=objFSO.getfile("c:users\%profile%documentsFile.txt")
set File4=objFSO.getfile("c:userspublicdocumentsFile.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 )
答案

我认为如果你使用ArrayList捕获每个文件的DateLastModified会更容易。 与VBScript数组不同,ArrayList允许您动态添加值。此外,ArrayList有一个非常有用的方法叫Sort(),我们可以在这里用来确定dateMindateMax值。

尝试:

Option Explicit

Dim objFSO, objList, objFile, arrFiles, fileName

arrFiles = Array("c:	empfile.txt","c:	estFile.txt","c:users\%profile%documentsFile.txt","c:userspublicdocumentsFile.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:	empfile.txt","c:	estFile.txt","c:users\%profile%documentsFile.txt","c:userspublicdocumentsFile.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

以上是关于VBscript创建数组但省略空白变量的主要内容,如果未能解决你的问题,请参考以下文章

vbscript 我的代码片段

vbscript 各种自定义代码片段 - 有关详细信息,请参阅注释

vbscript 使用此代码段从Excel中的数据行创建动态数组。

VBScript 创建一个多维数组并添加到它?

如何修复空白片段?

Sphinx - 在代码块片段中使用省略号 (...)