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

发布于 2024-08-26 23:20:20 字数 303 浏览 3 评论 0原文

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

(我正在寻找可以为我完成此操作的在线服务,但我认为在我的计算机上本地运行的脚本就足够了)。

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

如果有人有类似的东西并且可以贡献代码 - 我们将不胜感激!

D

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野却迷人 2024-09-02 23:20:20

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

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

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

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

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.

知足的幸福 2024-09-02 23:20:20

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

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

一个很好的功能是,除非文件内容发生更改,否则不会发生提交。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文