[xplorer˛] — WSH III: command line arguments
home » blog » 18 April 2010
 

Our previous article on windows scripts was about a simple visual basic script (VBS) that would recursively list the contents of a folder including its subfolders, which I repeat here:

REM SUBDIR.VBS: get a recursive listing a folder
call listdir("C:\TEMP")

Sub listDir(what)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDir = oFSO.GetFolder(what)

For Each f in oDir.Files
   Wscript.Echo what & "\" & f.name
Next

' recursively enter subfolders
For Each f in oDir.subfolders
   call listdir(what & "\" & f.name)
Next
end sub

This script is fixed and will only list the contents of a single folder C:\TEMP so it isn't very useful. The problem is that we have fixed the folder name in the script, but there is an easy way out with command line arguments. Instead of specifying the folder to browse in the script, we pass it as an argument when we call the script, e.g.

cscript SUBDIR.VBS C:\TEMP

This idea passing information from the command line will be familiar to programmers (e.g. main(argc, argv) in C language) and to people who did some work with old style DOS scripts. In VBS we can use the Arguments property to access this extra information within our script. Here is a test:

REM ARG.VBS: print command line arguments

Set objArgs = WScript.Arguments
For I = 0 to objArgs.Count - 1
   WScript.Echo objArgs(I)
Next

Save this script as ARG.VBS then pass a few arguments to it in a command window as such:
cscript ARG.VBS C:\TEMP second "third with spaces"

and see what you will get in the printout (if you are bored just click on the snapshot to the right :)

Notice that Arguments is an array property, and each item is accessed with a 0-based index (0 is the first element, 1 is the second...). The total number of arguments is in Arguments.Count. Armed with this information it is simple to modify the original script so that it reads the folder name from the command line:

REM SUBDIR.VBS: get a recursive listing a folder

if WScript.Arguments.Count = 0 then
   Wscript.Echo "please specify a folder name"
   wscript.quit
end if

call listdir(WScript.Arguments(0))

Sub listDir(what)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDir = oFSO.GetFolder(what)

For Each f in oDir.Files
   Wscript.Echo what & "\" & f.name
Next

' recursively enter subfolders
For Each f in oDir.subfolders
   call listdir(what & "\" & f.name)
Next
end sub

See how we use WScript.Arguments(0) to specify which folder to read. Also noteworthy is the validation of command line parameters. If the caller doesn't supply any folder name, then the Count property is 0 and we display an error message and quit.

Using scripts with FileSystemObject and Scriptable Shell Objects you can do pretty much everything you can imagine, just experiment and the sky will be your limit.

Post a comment on this topic

 

 

What would you like to do next?

Reclaim control of your files!
  • browse
  • preview
  • manage
  • locate
  • organize
Download xplorer2 free trial
"This powerhouse file manager beats the pants off Microsoft's built-in utility..."

download.com
© 2002—2010 Nikos Bozinis, all rights reserved