Creating rooted Windows Explorer windows

I like to use "rooted" Windows Explorer windows. A rooted explorer window does not show a folder's ancestors nor any other drives, Desktop, etc. You just see the folder and its decedents. For example, running "explorer /e,/root,c:\Program Files\Internet Explorer" from the command line (or the start menu's "Run..." menu item) will open a root explorer window. The "Internet Explorer" folder's content is on the right and the folder's decedent folders is on the left. Having many of these rooted explores seems to help me keep my desktop organized.

However, I don't want to have to use the command line to create these rooted explorers. I wanted a little GUI that would present a folder section dialog and then have it run explorer for the selected folder. I did this using Windows Scripting Host (WSH) because I knew it could do what I wanted and that everyone could use the result because everyone with Windows XP (and I assume Vista) has WSH. What I did not know how to do was how to program WSH.

When you don't know how to use a language or toolkit and you want to code something really quickly it is best to find code that does something similar to what you want and then hack it. I was lucky and found some sample code and below is the result. Save the code to the file "explore.vbe" and to run it just open it -- I have a shortcut in my Quick Launch toolbar.

set shell = WScript.CreateObject("WScript.Shell")
shell.run "explorer.exe /e,/root," & BrowseForFolder("Windows Explorer Root")

Function BrowseForFolder(strPrompt)
'Uses the "Shell.Application" (only present in Win98 and newer)
'to bring up a file/folder selection window. Falls back to an
'ugly input box under Win95.
'Shell32.ShellSpecialFolderConstants
Const ssfPERSONAL = 5 'My Documents
Const ssfDRIVES = 17 'My Computer
Const SFVVO_SHOWALLOBJECTS = 1
Const SFVVO_SHOWEXTENSIONS = 2
Dim sh, fol, fs, lngView, strPath
Set sh = CreateObject("Shell.Application")
If Instr(TypeName(sh), "Shell") = 0 Then
BrowseForFolder = InputBox(strPrompt, "Select Folder", CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName))
Exit Function
End If
Set fs = CreateObject("Scripting.FileSystemObject")
lngView = SFVVO_SHOWALLOBJECTS Or SFVVO_SHOWEXTENSIONS
strPath = ""
Set fol = sh.BrowseForFolder(&0, strPrompt, lngView, ssfDRIVES)
Err.Clear
On Error Resume Next
strPath = fol.ParentFolder.ParseName(fol.Title).Path
'An error occurs if the user selects a drive instead of a folder
If Err.Number <> 0 Then
BrowseForFolder = Left(Right(fol.Title, 3), 2) & "\"
Else
BrowseForFolder = strPath
End If
End Function
Wow, this is ugly code. But it does work.

No comments: