Rich Megginson ([info]richmegginson) wrote,
@ 2009-04-09 18:27:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Current music:"Mountain Waltz" - Steve Morse Band
Entry tags:windows wget unzip

How to replace wget and unzip in Windows
Many of our build tools on Windows could use plain old native cmd and nmake were it not for the need to use wget to download components and unzip to unzip them. I discovered how to replace these:

* bitsadmin - The Windows Support Tools package, available for most Windows versions, has a command line tool called bitsadmin. This (un)helpfully named command can be used to download files from http sites. Typical use in a batch file or Makefile is like this:

"c:\program files\support tools\bitsadmin" /wrap /transfer jobname /download /priority normal http://site/path/to/file c:\full\path\to\dest\filename

NOTES:
* You have to specify the full URL of the file to download - you cannot download recursively or with wildcards
* http is the only protocol I could get to work - no ftp or https - it's theoretically possible to get https working - for ftp, use the ftp built-in command
* You have to specify the full, absolute path of the destination, including file name - you cannot download to ., or to \destdir, you have to specify the full thing as drive:\full\path\to\destdir\filename - otherwise you will get cryptic, meaningless errors

* unzip.vbs - Windows has had built-in support for the zip compression mechanism for quite some time - if you've ever opened a compressed folder in explorer, you've seen this in action. Unfortunately, there is no simple unzip command line tool built-in. Fortunately, Windows has a powerful Visual Basic/JScript scripting language interpreter built-in called cscript. This allows you, through the magic of COM, to access many (not very well documented) Windows internals. Here is some cscript Visual Basic code I use to unzip files:

' get our FSO object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' src zip file is arg 0
SRC = objFSO.GetAbsolutePathName(objArgs(0))
' dest folder is arg 1 or "."
If objArgs.Count < 2 then
DEST = objFSO.GetAbsolutePathName(".")
Else
DEST = objFSO.GetAbsolutePathName(objArgs(1))
End If

' create dest folder if it does not exist
If not objFSO.FolderExists(DEST) Then
objFSO.CreateFolder(DEST)
WScript.Echo "Created new folder", DEST
End If

' see if file ends in .zip - if not (e.g. .jar) make temp copy
' that ends in .zip
Dim newSRC
newSRC = ""
If not Right(SRC, 4) = ".zip" Then
newSRC = SRC & ".zip"
objFSO.CopyFile SRC, newSRC, true
SRC = newSRC
End If

' get the shell application object used to do the unzip
Set objShell = CreateObject("Shell.Application")
Set objSrc = objShell.Namespace(SRC)
Set objDest = objShell.Namespace(DEST)
objDest.CopyHere(objSrc.Items)

' remove temp zip, if any
If Len(newSrc) > 0 Then
objFSO.DeleteFile(newSRC)
End If

WScript.Stdout.Write "Done. Copied contents of " & SRC & " to " & DEST

You run it like this: cscript //nologo file.vbs.  The first part is just standard stuff covered in the cscript getting started guide. The real magic happens with Shell.Application. This apparently uses the desktop/explorer directly, just as if you had opened and copied a compressed folder on the desktop. IF you have a large zip file, you can even see the progress window with the flying files.

Re-writing the VB script to JScript is left as an exercise to the reader.




(3 comments) - (Post a new comment)

Type Mismatch
(Anonymous)
2009-04-30 10:26 pm UTC (link)
I wanted to say thanks for taking the time to share your knowledge of this.

When I try and run this with a filename, I get an error: Type Mismatch: 'objArgs'. I assume objArgs is some kind of environment that doesn'tneed to be instantiated.

Thanks,
Brian

(Reply to this) (Thread)

Re: Type Mismatch
[info]richmegginson
2009-04-30 10:43 pm UTC (link)
Weird - what version of Windows are you using?

(Reply to this) (Parent)


(Anonymous)
2009-09-09 08:46 pm UTC (link)
Really crappy code. Why not use windows API instead of vbscript, doesn't even work and not a good example.

(Reply to this)


(3 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…