Computer Learning Zone CLZ Access Excel Word Windows

Learning is not attained by chance, it must be sought for with ardor and diligence.

-Abigail Adams
 
Home   Courses   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Tips & Tricks

New Tips Added Weekly!
Click here to get on our Mailing List

   
 
VBScript

Download a File

How to download a file.

In this tip you will learn how to Rename a file and move it to another folder to archive it.

The Code prompts for all the values.

Might be worth converting to an HTA file to make it easier.

	' ---------------------------------
	' Download a file from a website
	'
	' Author: Alex Hedley - 599CD.com
	' Created: 3rd November 2012
	' Updated: 
	' ---------------------------------

	 ' If you wanted the same thing everytime then you just need this one line of code.
	 ' It will overwrite the file if it already exists
	'download "http://599cd.s3-website-us-east-1.amazonaws.com/StudentFiles/Access/Access-2010-B1.zip", _
		"D:\599CD\Sample\Access-2010-B1.zip"

	'InputBox( prompt [, title] [, default] [, xpos] [, ypos] [, helpfile] [, context] )
	' If you wish to download the file more than once prompt for an amount of times to loop
	Dim TimesToDownload
	TimesToDownload = InputBox ("Loop for ? times", "Loop For?", "3")

	If TimesToDownload = "" Then
		Msgbox "You didn't chose a number.", vbCritical, "Quitting"
		WScript.Quit
	End If
		
	For i = 1 to TimesToDownload
		DownloadFile(i)
	Next

	' If you only wish to download it once you could comment out the above code and uncomment the below line.
	' Sending an empty string "" will keep the filename as "Test.exe"
	'downloadfile ""

	Msgbox "All Complete", vbInformation, "Complete"

	' ------------------------------
	' Script to choose a FileName
	' Calls the "download" function
	' ------------------------------
	Function DownloadFile(i)

		' Don't need a prompt for the Website as this is static, if you did you could do something like this:
		'Dim Website
		'InputBox( prompt [, title] [, default] [, xpos] [, ypos] [, helpfile] [, context] )
		'Website = InputBox ("Choose a Website Address" & vbNewLine & "and File to download:", "Website", _
			"http://599cd.s3-website-us-east-1.amazonaws.com/StudentFiles/Access/Access-2010-B1.zip")
		Website = "http://599cd.s3-website-us-east-1.amazonaws.com/StudentFiles/Access/Access-2010-B1.zip"
		
		' Static FilePath
		Dim FilePath
		FilePath = "D:\599CD\Sample\"
		
		Dim FileName
		' Increment by the loop "i"
		'InputBox( prompt [, title] [, default] [, xpos] [, ypos] [, helpfile] [, context] )
		FileName = InputBox ("Path: " & FilePath & vbNewLine & vbNewLine & "Choose a FileName" _
			, "FileName and Location", "Test" & i & ".exe")

		' Join the FilePath to the FileName
		Dim File
		File = FilePath & FileName

		' Check that the User has chosen both
		'If IsNull(Website) Or IsNull(FileName) Then
		If Website = "" Or FileName = "" Then
			'MsgBox( prompt [, buttons] [, title] [, helpfile, context] )
			MsgBox "You haven't chosen a Website or FileName", vbExclamation, "Error"
			WScript.Quit
		Else
			' Call the "download" function with the website and file
			download Website, File
			' Inform the user it is complete
			'MsgBox( prompt [, buttons] [, title] [, helpfile, context] )
			Msgbox "Complete", vbInformation, "Complete"
		End If

	End Function

	' --------------------------
	' Script to download a File
	' --------------------------
	function download(sFileURL, sLocation)
	 
		'create xmlhttp object
		Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
	 
		'get the remote file
		objXMLHTTP.open "GET", sFileURL, false
	 
		'send the request
		objXMLHTTP.send()
	 
		'wait until the data has downloaded successfully
		do until objXMLHTTP.Status = 200 :  wscript.sleep(1000) :  loop
	 
		'if the data has downloaded sucessfully
		If objXMLHTTP.Status = 200 Then
	 
			'create binary stream object
			Set objADOStream = CreateObject("ADODB.Stream")
			objADOStream.Open
	 
			'adTypeBinary
			objADOStream.Type = 1
			objADOStream.Write objXMLHTTP.ResponseBody
	 
			'Set the stream position to the start
			objADOStream.Position = 0    
	 
			'create file system object to allow the script to check for an existing file
			Set objFSO = Createobject("Scripting.FileSystemObject")
	 
			'check if the file exists, if it exists then delete it
			If objFSO.Fileexists(sLocation) Then objFSO.DeleteFile sLocation
	 
			'destroy file system object
			Set objFSO = Nothing
	 
			'save the ado stream to a file
			objADOStream.SaveToFile sLocation
	 
			'close the ado stream
			objADOStream.Close
	 
			'destroy the ado stream object
			Set objADOStream = Nothing
	 
		'end object downloaded successfully
		End if
	 
		'destroy xml http object
		Set objXMLHTTP = Nothing
	 
	End function

	' Sample
	'download "http://remote-location-of-file", "C:\name-of-file-and-extension"
	
Download Source

Source

 

Alex Hedley (Avatar) By: Alex Hedley


Click here to sign up for more FREE tips

 

 

 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2023 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 11/28/2023 9:37:52 AM.