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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Download a file with a redirect using VBScript

Status
Not open for further replies.

sugarflux

Technical User
Aug 14, 2003
111
GB
Hi Guys

Hoping someone may be able to help me. I'm trying to write a script to automatically download a file once a day from a site. It's a zipped csv file, usually around 6mb. The site is slow!

When i first hit the URL from my script a 303 response is returned which seems to be redirecting to a login page. However if I manually paste the url directly into a browser the file downloads ok (200 response).

Can anyone help me get to the bottom of this please?

Code:
set xHttp=createObject("Microsoft.XMLHTTP")
  xHttp.Open "GET","[URL unfurl="true"]http://xxxx.com/index.sjs?SomeParams,false[/URL]
  xHttp.Send
						
  strFilename="C:\testfile.zip"

  if xHttp.status=200 then							
    set bStrm=CreateObject("ADODB.Stream")
      with bStrm
        .type=1
        .open
        .write xHttp.responseBody
        .saveToFile strFilename,2
        .close
      end with
    set bStrm=nothing
  else 
    msgbox "Could not access server"
  end if
set xHttp=nothing
		
set fso=CreateObject("Scripting.FileSystemObject")
  if fso.fileexists(strFilename) then
    msgbox "Saved '" & strFilename & "' - successful"
  else msgbox "Something went wrong"
  end if
set fso=nothing

Thanks

~S~
 
Hi !
May be you must logon to your site and then you can download your file ?
I made a vbscript that can download a game in swf and play it with IE in a full screen, so you can take a look at this code and modify it for your purpose.

Code:
Option Explicit
Dim strFileURL,strHDLocation,Title,objFSO,PathScript,Question
Title = "Télécharger un Jeu Flash avec exécution par Vbscript © Hackoo Crackoo © 2011"
Set objFSO = Createobject("Scripting.FileSystemObject")
PathScript = objFSO.GetParentFolderName(wscript.ScriptFullName) 'Chemin ou se localise le Vbscript
' ---------------------------------Les paramètres à modifier pour télécharger le jeu---------------------------------
    strFileURL = "[URL unfurl="true"]http://www.jeuxclic.com/jeux/game-1270029047.swf"[/URL]
    strHDLocation = PathScript & "\Coast Bike.swf"
' ---------------------------------Les paramètres à modifier pour télécharger le jeu---------------------------------
'*********************************************************************************************************************
Call HTTPDownload(strFileURL,strHDLocation)
Question = MsgBox ("The File "& qq(strHDLocation) &" is Downloaded Successfully ! " & Vbcr & "Do you want open its location now ?",VBYesNO+VbQuestion,Title)
 If Question = VbYes then
    Call Explorer()
 End if
'***************************************************************************
Sub HTTPDownload(strFileURL,strHDLocation)
Dim ws,objXMLHTTP,objADOStream
    Set Ws = 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
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End If
Set objXMLHTTP = Nothing 
End Sub
'****************************************************************************
Function Explorer()
Dim LockDown,Keysec1,itemtype,WS
Set Ws = CreateObject("WScript.Shell")
'Autoriser le contenu actif à s'exécuter dans les fichiers de la zone Ordinateur local
 LockDown="HKLM\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN\"
 Keysec1=LockDown & "iexplore.exe"
 itemtype = "REG_DWORD"
 WS.RegWrite Keysec1,0,itemtype 
Ws.Run "%comspec% /c Start iexplore " & strHDLocation,0,1 
wscript.sleep 5000
WS.SendKeys "{F11}" 'pour mettre internet explorer en plein écran
Set WS = Nothing
end Function
'***************************************
Function qq(strIn)
    qq = Chr(34) & strIn & Chr(34)
End Function
'***************************************
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top