views:

292

answers:

3

Hi,

I need to daily run a script that will download a file from a fixed location and save it on my computer with an appropriate filename-YYYYMMDD-HHSS.ext timestamp. I need a historical record of what that file was at that particular time. I can manually check and see what the changes were, so compairson not needed.

(I was looking for an online service that would do this for me, but I think a locally running script on my machine would be good enough).

Although i do have php on my machine, i would prefer if its a pure windows builtin solution, just in case i have to (likely) adapt it to someone else's system (non-techies).

If someone has something like this and can contribute the code - help would be most appreciated!!

D

A: 

A version control system like Mercurial can do this for you without you having to rename the file. The script might be as simple as (get wget here and Mercurial here):

wget http://blah-blah-blah.com/filename.ext
hg commit -m "Downloaded new filename.ext"

A nice feature of this is that the commit won't happen unless the file's contents have changed.

To see the history, use hg log or TortoiseHg (a shell extension).

Marcelo Cantos
That's quite nice Marcelo, but i'm looking for a pure windows solution - sorry bud, this just wont fly for me... :(
Dave
A: 

Your task can be easily scripted using Windows Script Host languages -- VBScript or JScript.

To download a file from Internet, you can use the XMLHTTP object to request the file contents from the server and then use the ADO Stream object to save it to a file on the disk.

As for the timestamp, the problem is that neither VBScript nor JScript have built-in functions that would format the date in the format you need, so you will have to write the code for doing this yourself. For example, you could split the date into parts, pad them if necessary and concatenate them back together. Or you could use the WMI SWbemDateTime object that uses the yyyymmddHHMMSS.mmmmmmsUUU date format, and simply extract the yyyymmddHHMMSS part from it.

Anyway, here's a sample script (in VBScript) that illustrates the idea. I hard-coded the original file name in the strFile variable, because I was too lazy to extract in from the URL (and also in case the URL doesn't specify the file name, like in http://www.google.com).

Dim strURL, strFile, strFolder, oFSO, dt, oHTTP, oStream

strURL    = "http://www.google.com/intl/en_ALL/images/logo.gif"  ''# The URL to download
strFile   = "logo.jpg"    ''# The file name
strFolder = "C:\Storage"  ''# The folder where to save the files

Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2

''# If the download folder doesn't exist, create it
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not oFSO.FolderExists(strFolder) Then
    oFSO.CreateFolder strFolder
End If

''# Generate the file name containing the date-time stamp
Set dt = CreateObject("WbemScripting.SWbemDateTime")
dt.SetVarDate Now
strFile = oFSO.GetBaseName(strFile) & "-" & Split(dt.Value, ".")(0) & "." & oFSO.GetExtensionName(strFile)

''# Download the URL  
Set oHTTP = CreateObject("MSXML2.XMLHTTP")  
oHTTP.open "GET", strURL, False
oHTTP.send

If oHTTP.Status <> 200 Then
    ''# Failed to download the file
    WScript.Echo "Error " & oHTTP.Status & ": " & oHTTP.StatusText
Else
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Type = adTypeBinary
    oStream.Open

    ''# Write the downloaded byte stream to the target file
    oStream.Write oHTTP.ResponseBody
    oStream.SaveToFile oFSO.BuildPath(strFolder, strFile), adSaveCreateOverWrite
    oStream.Close
End If

Feel free to ask if you need more explanation.

Helen