4

I have a script that periodically downloads information from an RSS feed, one of which being an image. Right now, I'm checking to see if the image exists before I download it using the FileSystemObject and the FileExists comparison so that I'm not constantly downloading the same file over and over. Periodically, the image will update, but keep the same name, but after running some tests, it looks like FileExists just compares the filenames, not the actual file. Since the file online and the file locally have the same name, it won't download the image even though they are different images.

My question is is there another way to compare files to see if they're different despite the names?

This is the function I'm using:

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
Jonny
  • 65
  • 11
  • If an answer resolved your problem, please mark it as the accepted answer. If none of the answers helped, please edit your question and we might be able to help you. – Lews Therin Jun 20 '17 at 13:57
  • Based on the answers, it seems in order to compare the files fully you have to download the file regardless. So the question still somewhat remains, is there another aspect I can compare, file size maybe without downloading the file each time? – Jonny Jun 21 '17 at 12:09

2 Answers2

2

You could check the MD5 hash of the file.

See this question for details on how to implement this.

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 did not test this code, it is the code from the linked answer. I posted it in case the answer gets deleted or the link breaks.

Lews Therin
  • 3,707
  • 2
  • 27
  • 53
0

To compute the (new) hash of the (changed) external file, you would have to download it. If the external site does not publish/make accessible a time stamp or a hash, you'll have to download the file 'just in case of an update'.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • Sounds like I can't do a full compare without downloading the image each time. Do you think comparing file size would be a better alternative? I can't imagine there being much of a difference if the image is always the same size, just different content on it... I'm currently reading up on how to pull that information from the http request. – Jonny Jun 20 '17 at 14:43