Education is the most powerful weapon
which you can use to change the world.
-Nelson Mandela
Home   News   Courses   Tips   Templates   Forums   Help   Order   Contact   Logon  
Tips & Tricks

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


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 -
	' 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 "", _

	'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"
	End If
	For i = 1 to TimesToDownload

	' 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", _
		Website = ""
		' 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"
			' 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 "GET", sFileURL, false
		'send the request
		'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.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
			'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



Alex Hedley (Avatar) By: Alex Hedley

Click here to sign up for more FREE tips


You may want to read these articles from the 599CD News:


Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Account Login
Online Theater
Lost Password
Free Upgrades
Insider Circle
Student Databases
Change Email
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Waiting List
Production Schedule
Collapse Menus
Live Chat
Customer Support
WalkThru Tutorials
Consulting Services
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Video Tutorials
MYOLP Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Terms of Sale
Live Chat
General Info
Support Policy
Contact Form
Email Richard
Mailing Address
Phone Number
Fax Number
Course Survey
Facebook    Twitter

YouTube Channel    LinkedIn

Blog RSS Feed