Gargoyle
(MM club member)
2009-05-29 01:23 AM
Need to download from website in a script

Thought I had seen an UDF to allow the download of a file from a website. But can't seem to find it now.

Have to download a file from a website (they only make it available this way) once a month. The file name is always the same (except a scriptable portion of it).

Anybody know of an UDF or another way that I can do this.


AllenAdministrator
(KiX Supporter)
2009-05-29 01:43 AM
Re: Need to download from website in a script

This one?
FTPGet() - http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=83713


Gargoyle
(MM club member)
2009-05-29 02:28 AM
Re: Need to download from website in a script

See it was that FTP that got me....

Thanks.


NTDOCAdministrator
(KiX Master)
2009-05-29 02:30 AM
Re: Need to download from website in a script

Well not sure if this works but here is something I put together back in early 2004
You can see if you can modify it and update it to work for your neeeds.
This code is NOT what I would consider production quality but it works

 Code:
Dim $URL,$Link,$RC,$Page,$Server,$File,$Download,$Target,$Wait,$Servers1,$Servers2,$FileSize
$URL='http://www.sarc.com/avcenter/download/pages/US-SAVCE.html'
$Link='http://definitions.symantec.com/defs/xdb/'
$RC=readHTML($URL)
$File=InStr($RC,$Link)
$File=SubStr($RC,$File+41,12)
$Download = $Link + $File
$Target='C:\DOWNLOAD\'
$Servers1='AV01','AV02','AV03','AV04'
$Servers2='D09','D17'
 
? 'Downloading ' + $File + '  Please wait...' 
If FTPget($Download,$Target+$File)
  ? 'Error occured: ' + @ERROR + ' ' + @SERROR
Else
  ? 'Download complete... File saved in ' + $Target + ' as ' + $File
	$FileSize = GetFileSize($Target+$File)
	If $FileSize >14000000
		? 'Ready to update AntiVirus Defs'
		? 'File size downloaded was: ' + $FileSize
	  For Each $Server In $Servers1
	    Copy $Target+$File '\\'+$Server+'\c$\PROGRA~1\SAV'
	    ? 'Copying to ' + $Server +'...  ' + @ERROR + ' ' + @SERROR
	  Next
	  For Each $Server In $Servers2
	    Copy $Target+$File '\\'+$Server+'\c$\PROGRA~1\NAV'
	    ? 'Copying to ' + $Server +'...  ' + @ERROR + ' ' + @SERROR
	  Next
	  ; This line archives the definition files to AV01 for reference
	  MOVE $Target+$File '\\av01\c$\XDB\OLDXDB'
	    ? 'Moving xdb file to AV01 archive  ' +@ERROR + ' ' + @SERROR
	    ? 'Definition updates completed...' 
	    ? 'Please press a key to continue...'
	Else
			? 'The file size appears to be too small.  Update aborted, please try later...'
			? 'File size was: ' + $FileSize
	EndIf
	?? 'Press a key to continue...'
  GET $Wait
EndIf

Function readHTML($page)
Dim $obj
 $obj=createobject("microsoft.xmlhttp")
 $obj.open("get",$page,not 1)
 $obj.send
 $readHTML=$obj.responsebody
EndFunction

Function FTPget($sURL, $sTargetFile, optional $sUser, optional $sPass)
  Dim $oFTP, $oStream
  $sUser=""+$sUser
  $oFTP = CreateObject("Microsoft.XMLHTTP")
  if @error $ftpget=1 exit 1 endif
  $oStream = CreateObject("ADODB.Stream")
  if @error $ftpget=2 exit 2 endif
  if $sUser
    $oFTP.Open("GET", $sURL, not 1, $sUser, $sPass)
  else
    $oFTP.Open("GET", $sURL, not 1)
  endif
  if @error $ftpget=3 exit 3 endif
  $oFTP.Send
  $oStream.Type = 1
  $oStream.Mode = 3
  $oStream.open
  $oStream.Write($oFTP.responseBody)
  if @error $ftpget=4 exit 4 endif
  $oStream.SaveToFile($sTargetFile, 2)
  if @error $ftpget=5 exit 5 endif
  $oStream.Close
EndFunction


BradV
(Seasoned Scripter)
2009-05-29 11:48 AM
Re: Need to download from website in a script

A non-kix option is wget. I use it quite often to automate downloads from web pages.

http://www.gnu.org/software/wget/


Gargoyle
(MM club member)
2009-05-29 06:26 PM
Re: Need to download from website in a script

So I started with the FTPget and now McAfee thinks it is a virus. How fun is that.

Gargoyle
(MM club member)
2009-05-29 07:36 PM
Re: Need to download from website in a script

So reading throught the thread on FTPget() I see that McAfee has had this issue before. Now they have to fix it yet again. Such fun. Have a ticket open with them now.

Gargoyle
(MM club member)
2009-05-29 07:50 PM
Re: Need to download from website in a script

Found that by stripping off the ASE header info it would save without an issue (script settings region). But have run into the issue of getting error code 4 (unable to open connection). How can I debug what is causing this?

Gargoyle
(MM club member)
2009-06-04 11:17 PM
Re: Need to download from website in a script

Any help guru's ? I still get the error code 4, from the UDF, but am unsure how to understand why it is unable to open the connection.

AllenAdministrator
(KiX Supporter)
2009-06-04 11:33 PM
Re: Need to download from website in a script

I've never used it. Curious though... in the header it says error 4 is
"4 -ADO write failed". Not sure what that means either, but its different than "unable to open connection".


NTDOCAdministrator
(KiX Master)
2009-06-05 01:02 AM
Re: Need to download from website in a script

http://support.microsoft.com/kb/870669
http://www.w3schools.com/ADO/ado_ref_stream.asp


Gargoyle
(MM club member)
2009-06-05 01:07 AM
Re: Need to download from website in a script

Thanks Doc, I must have been googling the wrong keywords. Now Off I go to read some more.

Allen - Well I never claimed to be any good at lining things up..... \:\)