在 Windows Vista 机器上根据日期下载文件和重命名的脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2528608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:11:32  来源:igfitidea点击:

Script to download file and rename according to date on Windows Vista machine?

windowsscriptingwsh

提问by Dave

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.

我需要每天运行一个脚本,该脚本将从固定位置下载文件,并使用适当的文件名-YYYYMMDD-HHSS.ext 时间戳将其保存在我的计算机上。我需要该文件在那个特定时间的历史记录。我可以手动检查并查看更改内容,因此不需要比较。

(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).

虽然我的机器上确实有 php,但我更喜欢它是一个纯 Windows 内置解决方案,以防万一我必须(可能)将它适应其他人的系统(非技术人员)。

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

如果有人有这样的东西并且可以贡献代码 - 帮助将不胜感激!

D

D

回答by Helen

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

您可以使用 Windows 脚本宿主语言(VBScriptJScript)轻松编写任务脚本。

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

要从 Internet 下载文件,您可以使用该XMLHTTP对象从服务器请求文件内容,然后使用ADO Stream对象将其保存到磁盘上的文件中。

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 SWbemDateTimeobject that uses the yyyymmddHHMMSS.mmmmmmsUUUdate format, and simply extract the yyyymmddHHMMSSpart from it.

至于时间戳,问题在于 VBScript 和 JScript 都没有内置函数可以将日期格式化为您需要的格式,因此您必须自己编写代码来执行此操作。例如,您可以将日期拆分为多个部分,必要时填充它们并将它们重新连接在一起。或者您可以使用SWbemDateTime使用yyyymmddHHMMSS.mmmmmmsUUU日期格式的 WMI对象,然后简单地从中提取yyyymmddHHMMSS部分。

Anyway, here's a sample script (in VBScript) that illustrates the idea. I hard-coded the original file name in the strFilevariable, 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).

无论如何,这里有一个示例脚本(在 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

Feel free to ask if you need more explanation.

如果您需要更多解释,请随时询问。

回答by Marcelo Cantos

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 hereand Mercurial here):

像 Mercurial 这样的版本控制系统可以为您完成这项工作,而您无需重命名文件。脚本可能很简单(在此处获取 wget并在此处获取Mercurial ):

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).

要查看历史记录,请使用 hg log 或 TortoiseHg(shell 扩展)。