Click here to Skip to main content
15,892,697 members
Articles / Programming Languages / VBScript

Downloading files with VBScript

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
12 Dec 2012CPOL7 min read 114.4K   2   14  
How to download files with VBScript using COM (WinHTTP and MSXML) or WGET
strScriptFile = Wscript.ScriptFullName ' C:\download.vbs
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strScriptFile) ' Get file information from our Script.
strFolder = objFSO.GetParentFolderName(objFile) ' Get only folder from our script (C:)

Set objShell = CreateObject("WScript.Shell") ' Expand variables from command prompt
systemroot = objShell.ExpandEnvironmentStrings("%systemroot%") 

strLink = "http://download.windowsupdate.com/microsoftupdate/v6/wsusscan/wsusscn2.cab"
strSaveTo = "C:\"

' Use strFolder to save on the same location of this script
' WGet saves file always on the actual folder. So, change the actual folder for C:\, where we want to save file
objShell.CurrentDirectory = strSaveTo

' "C:\wget.exe" "http://download.windowsupdate.com/microsoftupdate/v6/wsusscan/wsusscn2.cab" -N -o C:\Windows\Temp\output.txt
'objShell.Run Quotes(strFolder & "\wget.exe") & " " & Quotes(strLink) & " -N -o " & systemroot & "\temp\output.txt",0,True
' -N: Continue download only if the local version is outdated.

objShell.Run Quotes(strFolder & "\wget.exe") & " --server-response --spider " & Quotes(strLink) & " -o " & systemroot & "\temp\output.txt",0,True

objShell.CurrentDirectory = strFolder

Set objFile = objFSO.OpenTextFile(systemroot & "\temp\output.txt")
			
Do
	line = objFile.ReadLine
	WScript.Echo line
			
	If InStr(line,"Content-Length") Then
		strContentLength = Mid(line,19,Len(line))
		' WScript.Echo strContentLength
    End If
			
Loop While Not objFile.AtEndOfStream
objFile.Close

' http://stackoverflow.com/questions/2942554/vbscript-adding-quotes-to-a-string
Function Quotes(strQuotes)
	Quotes = chr(34) & strQuotes & chr(34)
End Function 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions