Vbscript: Download files from a list of urls.

Posted: Friday, 26 April 2013

The following script can read through a list of urls [seperated by a return], and download them, for example, pretend that the following is a .txt file:

http://www.extramaster.net/LinesAndLinesOfCode.jpg
http://www.extramaster.net/GooglePlusBrand.jpg
It is then ran through the script, and "LinesAndLinesOfCode.jpg", "GooglePlusBrand.jpg" will be downloaded into the directory in which the script and the .txt file is placed in
The code is as follows:
Function ReplaceRegEx(origString,replaceString,replaceWith)
Set TempRegEx=New RegExp
TempRegEx.Pattern=replaceString
TempRegEx.IgnoreCase=True
ReplaceRegEx = TempRegEx.Replace(origString,replaceWith)
End Function
msgbox "Link Batch Downloader. Copyright (c) eXtraMaster, Andy Tran 2012, First Major scripting project in about a month..."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDictionary = CreateObject("Scripting.Dictionary")
currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
UserInput = InputBox("Where is the file containing the list of links named? (Must be in the same directory as this file)")
If UserInput = "" Then
msgbox "No Filename Entered"
Else
If objFSO.FileExists(currentDirectory & UserInput) Then
msgbox "File Entered"
ForReading = UserInput
Set objFile = objFSO.OpenTextFile(currentDirectory & UserInput,1)
i = 0
Do Until objFile.AtEndOfStream
strNextLine = objFile.Readline
If strNextLine <> "" Then
objDictionary.Add i, strNextLine
End If
i = i + 1
Loop
objFile.Close
msgbox "File has been read"
msgbox "Now Downloading.... (might take a while)"
currentDirectory=Mid(currentDirectory,1,Len(currentDirectory)-1)
For Each strLine in objDictionary.Items
Set objFSO = CreateObject("Scripting.FileSystemObject")
linkNameSplit=Split(strLine,"/")
linkNameNumber=UBound(linkNameSplit)
linkNameTemp=""
For i = 3 To linkNameNumber-1
linkNameTemp=linkNameTemp&"\"&linkNameSplit(i)
newfolderpath=currentDirectory & linkNameTemp
If Not objFSO.FolderExists(newfolderpath) Then
objFSO.CreateFolder(newfolderpath)
End If
Next
strFileURL = strLine
strHDLocation = currentDirectory & linkNameTemp &"\"& linkNameSplit(linkNameNumber)
msgbox "Downloading file from: " & strFileURL & vbCrLf & "Placing file at: " & strHDLocation
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
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End if
Set objXMLHTTP = Nothing
Next
msgbox "Download of Files Completed!"
Else
msgbox "File Does not Exist!"
End if
End if

Usage

To use it create a file with a file extension of  .vbs and paste the contents of the code below into the file you just created, or downloaded and extracted from the gist.
Then, put that file into any folder you want, create a new text file if you don't have the list of links yet, run the script and follow the prompts