[xplorer˛] — Scriptable Shell Objects
home » blog » 15 February 2009 [programming]


"There are only 10 types of people in the world;
those who understand binary and those who don't"

In the old days people didn't have tools like xplorer˛ so they had to resort to black magic to manage their files, using the cryptic language of MSDOS and its scripts. DEL /s c:\windows\*.* and such. For at least 10 years there is a modern object oriented alternative called windows script(ing) host (or WSH), a "proper" programming language offering access to standard objects to manipulate files and folders. Somehow it never quite caught on with people, if I judge from my own personal usage. I guess WSH is easy but using something like xplorer˛ is even easier <g>, so why waste brain waves?

If you think about it a file manager is a user friendly means to work with collections of files. Some people ask me why isn't there scripting support within xplorer˛ and my answer is always the same: if you want it easy, use xplorer˛, if you want to do something really complicated that requires scripting, then use WSH. There is no point to reinvent the wheel within xplorer˛. WSH is installed on all windows (95+) and all you need to use it is a text editor like notepad or editor˛.

It is not possible to provide a conscise introduction to WSH from this article so I will not even attempt it. If you have worked with visual basic (VBA for excel macros) or done some javascript for a website then you will feel at home with WSH. You can write your scripts in either visual basic or javascript so you have access to loops, logical branching, variables, string manipulation and so on. More importantly you have access to all progid-creatable objects on your system (eg see under HKCR registry hive) including a few offered by WSH itself. Through such objects, playing with their attributes and methods, you can do anything you can imagine.

The wealth of objects you can use with WSH can make it daunting but all you need to find is the object that does what you're after, then check its reference manual for properties and methods. These have self-explanatory names so they are easy to understand and use. Today we will focus on scriptable shell objects that help with complex file management situations, in a sense extending xplorer˛. Let's create our first VBScript file, it's easy! Open notepad and save this script as PROPERTY.VBS

REM property.vbs - show property page of C:\
Set objShell = CreateObject ("Shell.Application") 
Set objFolder = objShell.Namespace ("C:\") 
objFolder.Self.InvokeVerb("Properties")
wscript.sleep 10000

Double click on this VBS file and see what happens? We get the property page for C:\ folder. The most important step and the starting point for all shell manipulation is to use WSH CreateObject method to get an instance of the explorer's Shell.Application object (note this isn't the same as WScript.Shell the built-in WSH inferior shell object). The documentation for Shell.Application has pointed us to its Namespace method, which we use to get another object representing c:\ folder. From there we jumped to the FolderItem object using the folder's Self property, and finally used InvokeVerb to show the properties. When I started out this morning I knew nothing about any of these methods, I just looked them up and got going — it's that simple!

Starting from the root shell object we can do pretty much everything, just check the properties and methods of the FolderItem and Folder objects: browse the filesystem, get file information, copy/paste and perform all the actions from the shell context menu. A subset of these operations is offered by WSH FileSystemObject.

Let's do something a bit more advanced, show the details of the contents of C:\TEMP folder, here's DETAILS.VBS:

REM details.vbs - show detailed contents of folder
option explicit
Dim objShell, objFolder, columns(2), msg, dummy, i, objFile
REM wscript does late binding only?
Set objShell = CreateObject ("Shell.Application") 
Set objFolder = objShell.Namespace ("C:\temp") 
columns(0) = 0 ' name
columns(1) = 1 ' size
columns(2) = 19 ' comment
msg = ""

REM get column headers
For each i in columns
	msg = msg & objFolder.GetDetailsOf (dummy, i) & vbTab
Next
msg = msg & vbCrLf & "--------------------" & vbCrLf 

REM get details of all contents
For Each objFile in objFolder.Items 
	For each i in columns
		msg = msg & objFolder.GetDetailsOf (objFile, i) & vbTab
	Next 
	msg = msg & vbCrLf
Next 

Wscript.Echo msg

Not quite xplorer˛ but if you run this script you'll see item details as you see them in explorer columns. Here we are working with the same objects as before. The essense is the folder's GetDetailsOf method that shows information for each item given a column offset (19 is the comment column for windows XP, could be different in vista). Another useful method is Items collection that lists all the items in our C:\temp folder. The rest of the script is just formatting the message we show eventually using WSH Echo method.

So there you have it, windows scripting host is not rocket science, but a useful administrator tool. You can find more useful script samples in the script centre and a few more here. Now can you write a script that takes the name of each file and assings it as its comment? <g>

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—2009 Nikos Bozinis, all rights reserved