Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › VBScripting, not doing what it should haha
New Posts  All Forums:Forum Nav:

VBScripting, not doing what it should haha

post #1 of 3
Thread Starter 
Hi All

I suck at VBS, but I got this code below off the net and edited it a bit but I have run into a few problems, anyway heres the code:
Code:
Dim myURL
myURL = "http://wcms.intra.dpwn.net/wcmsrepo/repo71407202/repo23111119/repo18184730/repo166104958/repo22360874/repo52616837/repoFolder63654270/nz_fcl_tariff_9_06_11_.xls"

Dim WinHttpReq
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send

myURL = WinHttpReq.ResponseBody
If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.ResponseBody
    oStream.SaveToFile ("C:\Users\tbaker\Test\FCL Tariff.xls")
    oStream.Close
End If

So basically what I am trying to do is download a spread sheet from a site and save to a directory for easy reference, I have run into the following problems:

1. The script as it is runs, however it does not download the xls file, but instead it looks like it downloads a webpage from the site its hosted on, the thing that confuses me is that if I copy and paste that URL into IE, it will prompt to open/save file like it usually does, so the address is fine.

One thing that might be causing it is that the site is login required, I login in every morning however so I thought it would bypass this login in step cause I am already logged in. For example, if you copy and paste the URL it does go straight to the file, if I wasn't logged in it wouldn't it would redirect to the login page.

2. The file name will change, as you can see its done by date, so I expect this code would stop working as soon as the file name changes, I got the link from a hyperlink on a webpage, the hyperlink is always in the same place etc, is there a way to script in "clicking" that link as then it will always download the correct file? I have tried copy and pasting in the above URL but removing the file name so "...../repoFolder63654270/" but that doesn't work and results it redirecting to the home page.

So yeah thats where I am at, hoping you guys can help me as it seems if the 2 things above get solved then this would work perfectly.

Cheers
post #2 of 3
http://www.frank4dd.com/howto/various/microsoft-vbs-http-download.htm
Code:
The example VBS script below shows how to download and save a file from a remote webserver using Microsoft VBS scripting. It can handle http/https and user authentication, using the MSXML2.XMLHTTP.3.0 method. The method could also handle client cert authentication, if necessary.

Due to the widespread use of self-signed, untrusted certificates for enabling https, it might be necessary to explicitly disable the verification. an example line has been included below, and more information can be found here (Microsoft Dev Center - Desktop, XML DOM Methods, set- and getOption).

Code:
'======================================================================
' http-download.vbs 1.0 @2011 by Frank4dd http://www.frank4dd.com/howto
' This script demonstrates a file download from a webserver using http(s).
' It can easily be extended for using basic web authentication.
'
' This program comes with ABSOLUTELY NO WARRANTY. You may redistribute
' copies of it under the terms of the GNU General Public License.
'======================================================================

'======================================================================
' Global Constants and Variables
'======================================================================
Const scriptVer  = "1.0"
Const DownloadDest = "http://www.frank4dd.com/images/frank4dd-logo.gif"
Const LocalFile = "C:\tmp.gif"
'Const webUser = "username"
'Const webPass = "password"
Const DownloadType = "binary"
dim strURL

function getit()
  dim xmlhttp

  set xmlhttp=createobject("MSXML2.XMLHTTP.3.0")
  'xmlhttp.SetOption(2, 13056) 'If url https -> Ignore all SSL errors
  strURL = DownloadDest
  msgbox "Download-URL: " & strURL

  'For basic auth, use the line below together with user+pass variables above
  'xmlhttp.Open "GET", strURL, false, webUser, webPass
  xmlhttp.Open "GET", strURL, false

  xmlhttp.Send
  Wscript.Echo "Download-Status: " & xmlhttp.Status & " " & xmlhttp.statusText
  
  If xmlhttp.Status = 200 Then
    Dim objStream
    set objStream = CreateObject("ADODB.Stream")
    objStream.Type = 1 'adTypeBinary
    objStream.Open
    objStream.Write xmlhttp.responseBody
    objStream.SaveToFile LocalFile
    objStream.Close
    set objStream = Nothing
  End If


  set xmlhttp=Nothing
End function 

'=======================================================================
' End Function Defs, Start Main
'=======================================================================
' Get cmdline params and initialize variables
If Wscript.Arguments.Named.Exists("h") Then
  Wscript.Echo "Usage: http-download.vbs"
  Wscript.Echo "version " & scriptVer
  WScript.Quit(intOK)
End If

getit()
Wscript.Echo "Download Complete. See " & LocalFile & " for success."
Wscript.Quit(intOK)
'=======================================================================
' End Main
'=======================================================================

Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
post #3 of 3
Thread Starter 
Thanks for the above,

However this returns the same result as my code, and *.xls file that is not the spreadsheet but a saved webpage.

It also seems to generate a lot of prompts for me to click ok on, I don't want that, I'm planning eventually to have this vbs run in the background using windows task scheduler so yeah when I look for the spreadsheet its always there and up to date.

And I still don't think it addresses the changing file name thing? (well not that I can see in the code, I might be wrong).

Cheers
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › VBScripting, not doing what it should haha