VBS FileSystem object - FileExists, comparing more than just the file name

I have a script that periodically downloads information from an RSS feed, one of which is an image. Right now, I am checking if an image exists before loading it using a comparison of FileSystemObject and FileExists so that I do not constantly download the same file again and again. Periodically, the image is updated, but retains the same name, but after running some tests it looks like this: FileExists simply compares the file names, not the actual file. Since the online file and the local file have the same name, it will not load the image, even if they are different images.

My question is, is there any other way to compare files to see if they differ from the name?

This is the function I use:

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
+4
source share
2 answers

You can check the MD5 hash of the file.

For more information on how to implement this, see 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

Disclaimer . I have not tested this code, this is the code from the linked answer. I sent it in case the answer is deleted or the link breaks.

+1
source

To compute the (new) hash of the (modified) external file, you will have to download it. If the external site does not publish / make the timestamp or hash available, you will have to upload the file “only if updated”.

0

Source: https://habr.com/ru/post/1679653/


All Articles