VBScript – Passing Arguments

So you want to pass some arguments to your VBScript either with a shortcut or command prompt…

Why do you need this at all? let’s say I have some customer I want to deploy some software or do something on their system and as well all know (Or we might not… at least not me) there is no way to Compress/UnCompress (Zip’d) files via VBScript in a nice way.

Or I want to automate a procedure to upgrade and if needed do a downgrade.

So the idea here is simple, I create a VBScript that will preform an UnZip and I want to specify what file ti UnZip and where to extract it to (I know you can always use WinZip Self Extractor of some sort but this is just an example for Arguments).

So here goes, simple start of the script with some objects (if you want you can open explicits I don’t think it’s needed with this one)


set objShell = CreateObject("Wscript.Shell")

set objFSO = CreateObject("Scripting.FileSystemObject")

Next line is where we set to get arguments from the script


Set args = WScript.Arguments

Now i’m going to do some verification here that if no arguments are passed i’m going to show an error with a MSGBOX as you can see in the screenshot below, and when they hit OK i’ll quit the script.


If Wscript.Arguments.Count = 0 Then

MsgBox ("Add some arugments this will not work without them...." & vbNewLine & _

"Please add MainFile and Locaion do unzip like Script.vbs C:\Temp\Temp.zip C:\Temp\")

WScript.Quit

Here is where i’m going to assign the arguments to a value, each arogument is passed as a number where the first argument is (0) and moving on.

In this case my command would be UnZip.vbs C:\Temp\Upgrade1234.zip C:\Temp\SoftwareUpgrade1234\

So

args.Item(0) = C:\Temp\Upgrade1234.zip  <-- First one And args.Item(1) =  C:\Temp\SoftwareUpgrade1234\ <-- Second one I think you catch what i'm talking about...


Else

PathOfZipFile = args.Item(0)

ExtractZipTo = args.Item(1)

End If

And this is what I do to UnZip the files…. (simple but make sure that you create folders if needed ETC…)


set UnZipIt = CreateObject("Shell.Application")

set FilesInzip=UnZipIt.NameSpace(PathOfZipFile).items

UnZipIt.NameSpace(ExtractZipTo).CopyHere(FilesInzip)

So here is the whole code…. just so it’s easy on the copy/paste (That you can’t do on WP7… YET)


set objShell = CreateObject("Wscript.Shell")

set objFSO = CreateObject("Scripting.FileSystemObject")

Set args = WScript.Arguments

If Wscript.Arguments.Count = 0 Then

MsgBox ("Add some arugments this will not work without them...." &amp; vbNewLine &amp; _

"Please add MainFile and Locaion do unzip like Script.vbs C:\Temp\Temp.zip C:\Temp\")

WScript.Quit

Else

PathOfZipFile = args.Item(0)

ExtractZipTo = args.Item(1)

End If

set UnZipIt = CreateObject("Shell.Application")

set FilesInzip=UnZipIt.NameSpace(PathOfZipFile).items

UnZipIt.NameSpace(ExtractZipTo).CopyHere(FilesInzip)

This entry was posted in Development, Scripting and tagged , , , . Bookmark the permalink.