vbscript – VBS FileSystem对象 – FileExists,比不仅仅是文件名

我有一个脚本定期从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

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

点赞