Extending Just a Little Further: The Folder Options Control Panel Item (part 2)

Office Space

Hi folks. Thanks for joining us for the second edition of Scripting Eye for the GUI Guy. We’re going to build upon what we learned last time and explore how to add, delete and modify registered file extensions. Please email us at scripter@microsoft.com (in English, if possible) and let us know whether you enjoy the column and what you would like us to cover in the future.

On This Page

Extending Just a Little Further
Adding a New File Extension
Deleting a File Extension
Changing an Existing File Extension

Extending Just a Little Further

In the previous edition of GUI Guy, we examined the File Types tab of the Folder Options dialog box and learned how to gather the content displayed in the “Registered File Types” list box using a script. In this edition we’re going to take the next step and learn how to add and delete registered file extensions. We’ll also learn how to configure which program, by default, opens files with a given file extension.

Adding a New File Extension

Let’s start with adding a new file extension. Suppose you want the extension .text to be recognized as a text file. From the GUI, open the Folder Options dialog box (from Windows Explorer select Tools->Folder Options, or select Folder Options from Control Panel), navigate to the File Types tab and click the New button. Then, in the Create New Extension dialog box, enter “text” in the File Extension text box, and select Text Document from the Associated File Type drop down list box. (If you don’t see the Associated File Type drop down list box, click the Advanced button.) Normally, you would click OK and be done. Instead, click Cancel and we’ll write a script that accomplishes the same thing.

Folder Options Dialog

Clicking OK within the Create New Extension dialog adds a new registry key under HKLM\SOFTWARE\Classes (or, equivalently, under HKEY_CLASSES_ROOT). The name of the registry key is the file extension preceded by a dot (.) and the default value is the name of the associated file type – txtfile in this case.

Here’s a script that uses the WMI registry provider to make those same changes to the registry, thereby adding TEXT as a registered file extension.

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer  = "."
strExtension = "TEXT"
strFileType  = "txtfile"

Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
 strComputer & "\root\default:StdRegProv")
  
strKeyPath = "SOFTWARE\Classes\." & strExtension
objReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath

strNewKeyPath = "SOFTWARE\Classes\." & strExtension
strEntryName = ""
objReg.SetStringValue HKEY_LOCAL_MACHINE,strNewKeyPath,strEntryName,strFileType

The script uses the CreateKey method of the registry provider to add a subkey to SOFTWARE\Classes. The name of the subkey is the file extension stored in the strExtension variable, preceded by a dot (.). After running the script, here is the result reflected in the GUI.

Folder Options Dialog

After the subkey is created, the script uses the SetStringValue method of the registry provider to add a default value to the new key. The default value added is the file type to which the new extension maps, which is stored in the strFileType variable. There are a couple of things of note here: First, the strEntryName variable is set to an empty string to specify the default value; Second, the “txtfile” file type name is a preexisting, registered file type which you can “discover” by clicking through the Registry Editor (regedit.exe) and looking at the default values of other registered file extensions, or by running the final script provided in the previous GUI Guy article.

Deleting a File Extension

Using the GUI, if you want to delete a file extension mapping, you select the corresponding file extension from the registered file types list box and click the Delete button. The result is that the .TEXT subkey under HKEY_CLASSES_ROOT, and all of its contents, are deleted. Here is a script that does exactly that for the .TEXT extension we added with the previous script.

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strExtension = "TEXT"

Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
 strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\Classes\." & strExtension
objReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath

Changing an Existing File Extension

When we registered the .TEXT extension, we indicated that it was of type txtfile. After registering the extension, the GUI indicated that .TEXT files open with Notepad.

Folder Options Dialog

In the GUI, you can change the program that is used by default to open files with a given extension. Here’s the dialog box you are presented with when you click the Change button.

Folder Options Dialog

Once you change the program used to open this type of file, the file is no longer considered to be of type Text Document (the type mapped to “txtfile”). Rather, it is now a new type of file, a “TEXT File”. The following screenshot demonstrates this; it was taken after changing the program that a TEXT file opens with to Internet Explorer.

Folder Options Dialog

When this program mapping change is made, it’s stored in the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.TEXT registry key. The Application value in that key is set to the name of the new program.

The following script changes the default program used to open files with a given extension.

Const HKEY_CURRENT_USER = &H80000001
strComputer  = "."
strExtension = "TEXT"
strFileExtsKey = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\"
strNewProgram = "excel.exe"
 
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
 strComputer & "\root\default:StdRegProv")
  
strKeyPath = strFileExtsKey & "." & strExtension

strEntryName = "Application"
objReg.SetStringValue _
HKEY_CURRENT_USER,strKeyPath,strEntryName,strNewProgram

The script uses the SetStringValue method to update the Application value in the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.TEXT key. It updates the value to the program executable name stored in the strNewProgram variable (Excel.exe in this case). The following screenshot shows the GUI reflecting the mapping of the .TEXT extension to Microsoft Excel after running the script.

File Description in HKCR

Taken together, this GUI Guy article and the first one give you the basic information you need to get started managing file extensions using scripts. To really realize the power of the scripting approach, you need to move beyond targeting a single machine and manage sets of machines. The scripts you’ve seen in these two articles are easily modified to do just that. To target a single remote machine, just change the value of the strComputer variable that appears at the top of each of the scripts to the name of the remote machine. By default, the Scripting Guys tend to set that variable to dot (.). That is just a synonym for localhost. Experiment by changing one of the “read-only” scripts from the first column. Change the value of the strComputer variable to the NetBIOS name or IP address of a remote computer on your network on which you are a local administrator. (You need to be a local admin in order to remote manage a computer using WMI scripts.) Once you’ve gotten a script to run against a remote computer, check out the templates on the Script Center. They are there to guide you in altering your scripts to do things such as run against multiple computers or read input from text files.

We hope you GUI guys have enjoyed your first steps into scripting waters. Check back for the next issue where we’ll focus our scripting eye on the GUI that enables you to manage permissions on files and folders. Until then, thanks for reading and happy scripting.