Stuff
OS/400 Edition
Volume 2, Number 1 -- January 16, 2003

Extending the Windows Shell with VBScript


by Howard Arner

You may have noticed that when you are in Windows Explorer and want to create a new folder by clicking the File and New menu items, you have lots of file types on your computer, and that you get a huge list of different new things that you can create. It's a more than just a bit annoying to me, and I have always wished there were a new-folder menu item that would appear when you right-clicked a folder. If this is something you'd like to have, too, then read on.

display

Creating a Solution

On my computer, I have so many registered file types that the list is not manageable. I did go into the File Types box on the explorer and remove a lot of the listings, but it is still a pain not to have this as an option from the right-click menu in Explorer, so I sat down and found a way to add this item.

The solution I came up with was to use Windows Scripting Host. WSH is built into Windows and allows you to write powerful programs in either the JavaScript or the VBScript language. Notice that I said "powerful programs," because you can do a whole lot of things using VBScript or JavaScript and your Windows machine. I have written scripts that log on to my AS/400 and transfer data to Excel files, that change my password on the AS/400 to keep it in sync with my Windows password, that automate my backup operations, and synchronize my development directories on my server with my laptop. Windows Scripting Host is like the CL of the Windows operating system.

The listing below is my VBScript program that knows how to create directories.

on error resume next
Set objArgs = Wscript.Arguments
NDSTR = objargs(i) & "\New Folder"
NDSTR=inputbox( "Enter a Directory Name","Make New Folder",NDSTR)
if NDSTR<>"" then
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set f = fso.CreateFolder(NDSTR)
  if err.number <> 0 then
    msgbox err.description
  else  
    msgbox "Folder " & NDSTR & " created."
  end if
end if

Type the above program into NotePad and save it as MKD.VBS in your D:\ or C:\ directory. The program is now ready to use.

Let's examine this program, line by line, to see how it works. The first line tells WSH that if any errors occur during the execution of the program, the program should continue with the next step. This is very useful, as I do not want my program blowing up with an error; I just want it to continue on so I can check the error collection and see what the error was.

The next line sets a variable called objArgs to be an array of the arguments passed to the script. When this script is invoked (more on how the script gets invoked later), Windows will pass the name of the folder as the first argument to this script; by executing this line of code, I now have any arguments passed to the script in an array.

On the next line, I set the variable NDSTR to the contents of the first argument, objArgs(0), concatenated with the string \New Folder. The script then proceeds to the next line, which uses the InputBox function to prompt for the name of the directory you wish to create. Once the user changes/types the directory name they desire, he can press OK and the value that was typed will be placed in the NDSTR directory. If the user cancels the operation, the NDSTR variable will be set to an empty string.

Next, the program checks that NDSTR is not an empty string and then proceeds to create an instance of the FileSystemObject into the variable fso. The FileSystemObject has a variety of properties and methods that allow you to interface with the Windows File System. You can use the File System Object to create folders, delete files, enumerate files, and a host of other things. For more information on the FileSystemObject, see the Windows Scripting Host documentation in the Windows Platform SDK.

Next, the program uses the CreateFolder method of the FileSystemObject to create a folder with the path defined in the NDSTR variable. Note that the syntax is SET F=fso.CreateFolder(NDSTR). You must use the SET syntax because the CreateFolder method returns a folder object. I don't need the folder object returned by the CreateFolder method, as I just want a folder created, but I have to put the created folder in a variable or the call will not work, hence the use of the SET syntax.

Next, I check the Number property of the Err object. If the Number is any value other than 0, an error occurred during the creation of the folder, so my program grabs the error description and uses the MsgBox function to tell me about the error. If the Number is 0, the program uses the MsgBox function to inform me that the directory was created. Simple script, eh?

Invoking the Script as a Menu Option

Before we get too much farther along, let's talk about how Windows shows the list of actions available for items in the Windows Explorer. When you right-click a file in the Windows Explorer, it uses the windows registry to decode what options are available for that file type. For example, when you right-click a .TXT file in the Explorer, you will see the menu option Open, which allows you to edit the text file with NotePad. If you want to add another action option to the menu, you need to change the windows registry to associate the action you desire with the file type .TXT. This association is easy to do because Microsoft provides an easy interface to this portion of the registry via the Tools and Folder Options menu items in Windows Explorer.

Clicking Tools, then Folder Options shows you the Folder Options dialog box. Click the File Types tab and you will see a screen like the one shown here:

Figure 1

Note that I have already scrolled the screen down to the N/A Folder entry in the registered file types list. This entry controls what options are available for folders when you right-click one in Explorer. Click the Advanced button and the following screen will appear:

Figure 2

Notice that I have an entry called Make Directory. To add this entry to your options, press the New button and fill in the following screen:

Figure 3

The Action field is text that you want to appear in the right-click menu. The Application used to perform action box contains the program to execute if the menu item is selected. In the Application used to perform action box, enter the following:

D:\Windows\System32\Wscript.exe D:\MKD.VBS "%1"

The first part of the command tells the operating system to execute the Wscript.EXE program. WScript.EXE is the Windows Script interpreter and is usually located in the System32 directory under your Windows directory. Notice I used the D: drive, as my Windows directory is on my D:, not my C:, drive, as is usually the case. The next portion of the command is the name of the script that I want the WScript program to run: in this case my program MKD.VBS stored in the root directory on my D: drive. Finally, the %1 is a special marker that tells Windows to pass the name of the currently highlighted objects to the WScript program. For example, if you highlight a folder called AS4Stuff on your C: drive, right-click and select the new option Make Directory from the menu, and Windows will pass the string C:\AS4Stuff to the script. You should always enclose the %1 in quotation marks, because this will ensure that Windows passes arguments with an embedded space correctly to the calling program. For example, if you highlighted the folder Program Files and passed it to the command with a %1 rather than a "%1", the program would receive Program as the first argument and Files as the second. The "%1" ensures that the program would receive the name Program Files as the first argument.

Press the OK button to close this screen, then press the OK button on the File Types display to return to Windows Explorer. Select a folder where you wish to create a directory and then right-click; you should see something like the following:

Figure 4

Select the Make Directory menu item and the following dialog should appear:

Figure 5

Change the name of the directory, if you desire, and press OK. The directory will be created!

Final Thoughts

If you want to, you can look in the registry and see how Windows stores this extension to the shell. Use RegEdit and navigate to the HK_CLASSES_ROOT/Folder/shell keys and take a look at the new registry entry for Make Directory.

Figure 6

The Windows shell is an extensible object, and the combination of VBScript and the FileSystemObject can let you create programs to automate mundane tasks or make the interface more suit your, or your clients', needs. For more information, go to the Microsoft Scripting site, at www.microsoft.com/scripting, or check out the Platform SDK documentation on the Windows shell.


Howard F. Arner, Jr., is a writer and consultant for Client Server Development in Jacksonville, Florida. Find out more about VBScript from his Web site, www.sqlthing.com, or buy a copy of his book iSeries and AS/400 SQL at Work from www.sqlthing.com/books. Howard can be reached at harner@itjungle.com.


Sponsored By
PROFOUND LOGIC SOFTWARE

Don't be left behind!

Thousands of programmers have adopted RPG-Alive, and are now able to read and understand RPG code 2 to 3 times faster.

To try RPG-Alive on your system, visit http://www.RPGAlive.com/now

"I am very happy with RPG-Alive! It's a terrific productivity booster!" says Brian Johnson of Help/Systems.

See other user testimonials at http://www.rpgalive.com/testimonials.html


THIS ISSUE
SPONSORED BY:

ASNA
COMMON
WorksRight Software
Profound Logic Software


BACK ISSUES

TABLE OF
CONTENTS
Back To Basics: The Date Data Type

Supercharge Your Search and Replace Tasks

Tired of Resetting Terminals on the AS/400? Let Somebody Else Do It

Extending the Windows Shell with VBScript


Editors
Shannon O'Donnell
Kevin Vandever

Managing Editor
Shannon Pastore

Contributing Editors:
Howard Arner
Joe Hertvik
Ted Holt
David Morris
Richard Shaler

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com


Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.