VBS 文件系统对象 - FileExists,比较的不仅仅是文件名

VBS FileSystem Object - FileExists, comparing more than just the filename

我有一个脚本可以定期从 RSS 提要下载信息,其中之一是图像。现在,我正在使用 FileSystemObject 和 FileExists 比较检查图像是否存在,然后再下载它,这样我就不会不断地一遍又一遍地下载同一个文件。图像会定期更新,但保持相同的名称,但在 运行 一些测试之后,看起来 FileExists 只是比较文件名,而不是实际文件。由于网上的文件和本地的文件重名,所以即使是不同的图片也不会下载。

我的问题是有没有另一种方法可以比较文件以查看它们是否与名称不同?

这是我正在使用的函数:

function saveImageReturnPath(oPath)
dim oFSO
dim oHTTP
dim oStream
dim fol
dim fil

set oFSO = createObject("Scripting.FileSystemObject")
fil = oFSO.getBaseName(oPath) & ".jpg"

if not oFSO.fileExists(localPath & fil) then
    set oHTTP = createObject("MSXML2.XMLHTTP")
    oHTTP.open "GET", oPath, false
    oHTTP.send
    set oStream = createObject("ADODB.Stream")
    oStream.type = 1
    oStream.open
    oStream.write oHTTP.responseBody
    oStream.saveToFile oFSO.buildPath(localPath, fil), 2
    oStream.close
end if

saveImageReturnPath = localPath & fil
end function

您可以检查文件的MD5 hash

有关如何实现此功能的详细信息,请参阅 this question

Set fso = CreateObject("Scripting.FileSystemObject")
Dim oMD5:  Set oMD5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")

Function GetMd5(filename)
    Dim oXml, oElement

    oMD5.ComputeHash_2(GetBinaryFile(filename))

    Set oXml = CreateObject("MSXML2.DOMDocument")
    Set oElement = oXml.CreateElement("tmp")
    oElement.DataType = "bin.hex"
    oElement.NodeTypedValue = oMD5.Hash
    GetMd5 = oElement.Text
End Function

免责声明:我没有测试这段代码,它是来自 linked 答案的代码。我发布它以防答案被删除或 link 中断。

要计算(更改的)外部 文件的(新)哈希值,您必须下载它。如果外部站点 publish/make 无法访问时间戳或哈希,则必须下载文件 'just in case of an update'.