您的任务可以使用 Windows 脚本宿主语言轻松编写脚本 -- VBScript 或 JScript。
要从 Internet 下载文件,您可以使用 XMLHTTP 对象向服务器请求文件内容,然后使用 ADO Stream 对象将其保存到磁盘上的文件中。
至于时间戳,问题在于 VBScript 和 JScript 都没有内置函数可以将日期格式化为您需要的格式,因此您必须自己编写代码来执行此操作。例如,您可以将日期拆分为多个部分,必要时填充它们并将它们连接在一起。或者,您可以使用使用 yyyymmddHHMMSS.mmmmmmsUUU 日期格式的 WMI SWbemDateTime 对象,然后简单地从中提取 yyyymmddHHMMSS 部分。
无论如何,这里有一个示例脚本(在 VBScript 中)说明了这个想法。我将原始文件名硬编码在 strFile 变量中,因为我懒得从 URL 中提取(而且如果 URL 没有指定文件名,例如在 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
如果您需要更多解释,请随时询问。