Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript to Automate File Downloading from a Website

Status
Not open for further replies.

hackoo

Programmer
Jan 31, 2003
28
TN
Hi everybody :) I post this VBscript that works for me so fine when i run it as a vbs file !
Code:
' Set your settings
    strFileURL = "[URL unfurl="true"]http://hackoo.ifrance.com/Merlin.jpg"[/URL]
    strHDLocation = "c:\Merlin.jpg"
' Fetch the file
    Set Ws = WScript.CreateObject("WScript.Shell")
    Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
    objXMLHTTP.open "GET", strFileURL, false
    objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0    'Set the stream position to the start
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End If
Set objXMLHTTP = Nothing 
Ws.Run strHDLocation
Set WS = Nothing
But my problem how can i make it works in HTML File ? because it didn't works for me i don't know why ? could someone here tell me what's wrong in the HTML file :(

Code:
<html>
 <head>
 <script language=vbscript>
 Sub Window_Onload
 Call Download_Me()
 End Sub
 Sub Download_Me()
 on error resume next
    strFileURL = "[URL unfurl="true"]http://hackoo.ifrance.com/Merlin.jpg"[/URL]
    strHDLocation = "c:\Merlin.jpg"

    Set Ws = WScript.CreateObject("WScript.Shell")
    Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
    objXMLHTTP.open "GET", strFileURL, false
    objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0    'Set the stream position to the start
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End If
Set objXMLHTTP = Nothing 
Ws.Run strHDLocation
Set WS = Nothing
 End Sub
 </script>
 </head>
 <body>
 <br>
 <br>
 <br>
 <br>
 <input type=button value="Click to Run" name="Download_Me" onclick="Download_Me()">
 </body>
 </html>
 
Replace this:
WScript.CreateObject
with this:
CreateObject

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Replace this:
WScript.CreateObject
with this:
CreateObject
Thank you for your reply but this still don't works.
I think this comes from the security issue so i'm working under Internet explorer version 7 and using Windows xp SP3.
I made some modification in the HTML file and even this don't works :(
Code:
<html>
 <head>
 <script language=vbscript>

 Sub Window_Onload
 Call Download_Me()
 End Sub

 Sub Download_Me()
 Dim strFileURL,strHDLocation,LockDown,Keysec1,itemtype
'Authorize the active contents to run in the files of the zone local Computer and the execution of the scripts.
'Autoriser le contenu actif à s'exécuter dans les fichiers de la zone Ordinateur local et l'exécution des scripts.
LockDown="HKLM\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN\"
Keysec1=LockDown & "iexplore.exe"
itemtype = "REG_DWORD"
    strFileURL = "[URL unfurl="true"]http://hackoo.ifrance.com/Merlin.jpg"[/URL]
    strHDLocation = "c:\Merlin.jpg"
    Set Ws = CreateObject("WScript.Shell")
	Ws.RegWrite Keysec1,0,itemtype 'la valeur 0 pour Autoriser l'exécution des scripts.
                                  'la valeur 1 pour Bloquer l'exécution des scripts.
    Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
    objXMLHTTP.open "GET", strFileURL, false
    objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 2 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0    'Set the stream position to the start
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End If
Set objXMLHTTP = Nothing 
Ws.Run strHDLocation
Set WS = Nothing
 End Sub
 </script>
 </head>
 <body>
 <br>
 <input type=button value="Click to Run" name="Download_Me" onclick="Download_Me()">
 </body>
 </html>
 
What about an HTA instead of HTML ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Also you don't need those, use the save option.
[tt]
[red]'[/red]Set objFSO = Createobject("Scripting.FileSystemObject")
[red]'[/red]If objFSO.Fileexists(strHDLocation) Then[red]'[/red]objFSO.DeleteFile strHDLocation
[red]'[/red]Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation[red],2[/red] '2=adSaveCreateOverWrite
[/tt]
 
You write thing to registry with html on the client, then you need definitively hta no matter what. Independently of switching to hta, that is already a bad idea.

If you need to change the registry, change it once and period (as I don't see you restore to the original.) That action should not be mixed with xmlhttp functionality in one single script. Just like using adodb.stream on the client machine need permission set in the registry on the client machine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top