如何解决VBScript中的文件下载路径错误?

时间:2017-08-12 18:36:45

标签: vbscript

我正在运行以下VBScript程序download.vbs

dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "http://websitelink/textfile.txt", False
xHttp.Send

with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "c:\dump.txt", 2 '//overwrite
end with

在此程序中,它将从给定的Web URL下载文本文件,并将其保存到给定路径中的本地磁盘。

但问题是,当我将下载路径更改为本地磁盘d:

时,它运行良好

并在使用本地磁盘c:作为下载路径时显示以下错误。

VBScript错误消息,

Script:     C:\test\download.vbs
Line:       10
Char:       5
Error:      Write to file failed.
Code:       800A0BBC
Source:     ADODB.Stream

请帮忙。我将非常感激。

1 个答案:

答案 0 :(得分:0)

如果文件不存在 - 您希望在safetofile上使用1。您可以添加一些代码

Const AdTypeBinary = 1, AdTypeText = 2
Const FsoRead = 1, FsoWrite = 2, FsoAppend = 8
Const BstrmCreate = 1, BstrmOvrWrt = 2
dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "http://websitelink/textfile.txt", False
xHttp.Send
mysavefile = "D:\dump.txt" 'Set your save-file here 



with bStrm
    .type = AdTypeBinary ' YOU MAY WANT TO CHANGE FILE TYPE FOR TEXT/BINARY
    .open
    .write xHttp.responseBody

    if ( FSO.FileExists( mysavefile ) ) then
        .savetofile mysavefile, BstrmCreate
    else
        .savetofile mysavefile, BstrmOvrWrt
    end if
end with