tools/w32DownloadUnzip.vbs
author Wuzzy <Wuzzy2@mail.ru>
Tue, 17 Jul 2018 21:31:41 +0200
changeset 13507 99a99dbfbf6d
parent 10017 de822cd3df3a
permissions -rw-r--r--
Update server dependencies in INSTALL.md
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
     1
' w32DownloadUnzip.vbs
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
     2
'   Download a zipfile and uncompress it with no external tools in Windows
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
     3
'
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
     4
' Copyright (c) 2012, Vittorio Giovara, <vittorio.giovara@gmail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
     5
' Redistribution and use is allowed according to the terms of the BSD license.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
     6
'
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
     7
' References
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
     8
'   http://superuser.com/questions/59465/is-it-possible-to-download-using-the-windows-command-line
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
     9
'   http://stackoverflow.com/questions/1021557/how-to-unzip-a-file-using-the-command-line
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    10
'   http://stackoverflow.com/questions/424331/get-the-current-temporary-directory-path-in-vbscript
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    11
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    12
Set ArgObj = WScript.Arguments
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    13
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    14
If (Wscript.Arguments.Count = 1) Then
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    15
    strFileURL = ArgObj(0)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    16
    strOutputPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    17
Else
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    18
    If (Wscript.Arguments.Count = 2) Then
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    19
        strFileURL = ArgObj(0)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    20
        strOutputPath = ArgObj(1)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    21
    Else
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    22
        WScript.Echo ("Usage: csript.exe w32DownloadUnzip.vbs url output")
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    23
        WScript.Quit
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    24
    End if
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    25
End if
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    26
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    27
' Temporary directory
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    28
strHDLocation = WScript.CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2) + "\hwlibtemp.zip"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    29
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    30
' Fetch the file
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    31
WScript.Echo ( "Trying to download from " & strFileURL)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    32
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    33
objXMLHTTP.open "GET", strFileURL, false
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    34
objXMLHTTP.send()
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    35
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    36
If objXMLHTTP.Status = 200 Then
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    37
    Set objADOStream = CreateObject("ADODB.Stream")
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    38
    objADOStream.Open
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    39
    objADOStream.Type = 1 'adTypeBinary
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    40
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    41
    objADOStream.Write objXMLHTTP.ResponseBody
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    42
    objADOStream.Position = 0 'Set the stream position to the start
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    43
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    44
    Set objFSO = Createobject("Scripting.FileSystemObject")
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    45
    If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    46
    Set objFSO = Nothing
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    47
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    48
    objADOStream.SaveToFile strHDLocation
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    49
    objADOStream.Close
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    50
    Set objADOStream = Nothing
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    51
    Set objXMLHTTP = Nothing
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    52
Else
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    53
    WScript.Echo ("Error downloading file (error code: " & objXMLHTTP.Status & ")")
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    54
    Set objXMLHTTP = Nothing
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    55
    WScript.Quit
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    56
End if
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    57
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    58
WScript.Echo ( "Extracting file to " & strOutputPath)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    59
Set objShell = CreateObject( "Shell.Application" )
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    60
Set objSource = objShell.NameSpace(strHDLocation).Items()
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    61
Set objTarget = objShell.NameSpace(strOutputPath)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    62
intOptions = 16 'no user prompt
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    63
objTarget.CopyHere objSource, intOptions
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    64
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    65
WScript.Echo ( "Success!" )
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 9069
diff changeset
    66
Set objShell = Nothing