Burning CDs in Windows XP with WSH

I see questions all the time from users who are trying to find a way to burn CDs in WSH. In Windows XP, there is no real direct way of doing this using scripting, but that certainly doesn’t mean it’s impossible! At times like this, you have to be creative and develop your own workaround.

Whenever I begin to approach a problem like this, I try to think of the most direct solution first.  Since scripting provides access to system APIs, I generally research those first.  Generally, the documentation will tell you if there is a scriptable interface that provides access.

Research will show that CD burning is a built-in feature in Windows XP.  The Windows operating system provides programmatic access to this feature through the Disc Imaging API, known as IMAPI.  The IMAPI control is a COM-enabled object, so programs written in VisualBasic, .NET, and the like can easily take advantage of Windows XP’s CD burning capabilities.

Unfortunately, the IMAPI object does not provide a suitable scripting interface.  This essentially means that you will not be able to use the IMAPI control directly from your scripts.  The next step toward a solution would be to find a third party application that will provide access to this feature for us.

At this point, you have some flexibility.  Windows XP Support Tools provides CDBurn.exe.  Many programs, such as Roxio and Nero, also provide scriptable controls you could implement.  These all make acceptable solutions; however, I tend to shy away from solutions that require installation of additional software since this limits the compatibility of a script.

Since I’ve decided not to use a third-party component, I finally look to automation.  Windows XP provides the CD Writing Wizard for burning files to a CD.  With this method, files can be copied to a virtual directory where they are staged for burning.  The wizard then takes you through the process of burning the CD.

{mospagebreak title=Behind the CD Writing Wizard}

The CD Writing Wizard in Windows XP doesn’t actually write CDs.  Instead, it provides a GUI interface to the underlying shell APIs.  Automating this wizard then becomes the only native way of accessing the necessary APIs from a script.

The WScript Shell object provides methods of automating GUI elements.  It allows you to activate open windows on the system and send keystrokes to those applications.  By taking advantage of keyboard shortcuts, you can respond to the user dialogs provided by the CD Writing Wizard and effectively automate it so that it can be run unattended from a script.

Using the CD Writing Wizard is pretty simple.  You can select the files or directories you wish to burn from My Computer or Windows Explorer.  You can then copy them to a temporary virtual directory by selecting your CD device from the Send To menu.  This is the virtual directory where the CD Writing Wizard looks for files when creating a CD image to burn.  The location of this directory is found in the registry key below.

HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell FoldersCD Burning

In order to automate this wizard, we first need to determine the location of the virtual CD directory by reading the registry key above.  Then, we can copy the files we wish to burn to that directory.  Finally, we can launch the CD Wizard and move through it by sending keystrokes to the application, effectively simulating the steps a user would take to perform the same action.

strDriveLetter = "E:"


strSourceDirectory = "C:Documents and SettingsNilpoMy Documents"

strCDName = "MyDocs"

We’re now ready to begin putting our script together.  The example script I’ll be creating in this article is going to burn a directory of files.  Start by creating a few necessary variables that include the drive letter of the CD-RW device, a directory path containing the files to be written to CD, and a name for the CD.

The CD name should be limited to sixteen characters or less.

If you are using this in your script as a method of creating backups, you may wish to consider using the current date as a part of your CD name.

{mospagebreak title=Building the Script}

Now we are ready to begin the process of preparing the files to be written to CD.

Set WshShell = WScript.CreateObject("WScript.Shell")

Set objShell = CreateObject("Shell.Application")


strBurnDirectory = WshShell.RegRead( _

   "HKCUSoftwareMicrosoftWindowsCurrentVersion" _

       & "ExplorerShell FoldersCD Burning")

You’ll need to create a reference to the WScript Shell object as well as the Shell Application object.  The WScript Shell object’s RegRead method is then used to determine the location of the CD staging directory by reading the registry key mentioned earlier.

Set objFolder = objShell.Namespace(strSourceDirectory)

Next, the Shell Application object’s Namespace method is used to return a folder object that represents the directory containing our source files.

objShell.Namespace(strBurnDirectory).CopyHere objFolder.Items

Now the source files are copied to the CD staging area. This line is a little complex so let’s break it down one piece at a time.

Again, the Namespace method is used to return a Folder object that represents the CD staging area.  The Folder object provides a CopyHere method that is used to copy a collection of files into the Folder object.  We provide that collection of files by using the Items property from the source Folder object.

With files in the staging area, you should now see a familiar balloon in the system tray indicating that there are files waiting to be written to CD.  Ordinarily, you would simply click the balloon to launch the CD Writing Wizard, but we cannot do that from a script, so we have to use a slightly different approach.

objShell.NameSpace(&H11&).ParseName(strDriveLetter).InvokeVerbEx( _

   "Write &these files to CD")

When there are files waiting in the staging area, you can open My Computer and right-click your CD-RW drive letter.  You will see “Write these files to CD” on the context menu.  This entry will launch the CD Writing Wizard so we need to simulate this action.

We start off by using the Shell Application object’s Namespace method to create a Folder object of My Computer.  The &H11& is a special folder code that represents the My Computer virtual folder.

Next, the ParseName method is used to bind to an item within a folder object.  In this case we’re using it to return a FolderItem object that represents the CD-RW drive letter entry.

Finally, the FolderItem object’s InvokeVerbEx method is used to execute a command from the item’s context menu.  The string provided here must match the command entry exactly.  It is case sensitive.  You’ll also notice that extra ampersand.  That represents the underlined letter (keyboard shortcut).  This must also be provided exactly.

{mospagebreak title=Finishing Up}

We’ve copied all of the files we wish to burn to the temporary staging area used by the CD Writing Wizard and launched the wizard.  Now we need to automate the steps of moving through the wizard.

Do Until WshShell.AppActivate("CD Writing Wizard")

   WScript.Sleep 200


The WScript Shell object provides an AppActivate method that is used to bring an open window into focus.  By creating a loop we can ensure that our script does not continue until this action is actually possible.  This acts as a sort of timer that waits until the CD Writing Wizard is actually open on the system.

WshShell.AppActivate("CD Writing Wizard")

WshShell.SendKeys strCDName

With the wizard open we can begin sending keystrokes to fill in the text fields in the wizard.  The wizard opens with the first text field in focus, so we can begin sending keystrokes right away using the WScript Shell’s SendKeys method.

It’s a good practice to activate the Window before attempting to send any keystrokes.  This ensures that the correct window has focus when we begin typing the name of the CD to create.

WshShell.AppActivate("CD Writing Wizard")

WshShell.SendKeys "{Enter}"

Now we need to continue to the next screen in the wizard.  Simply pressing the Enter key will do that, so we can send that as a keystroke — again, making sure that the window is activated first.

Do Until Not WshShell.AppActivate("CD Writing Wizard")

   WScript.Sleep 200


This will begin the CD writing process.  Now we have a little problem.  If we allow the script to exit, it will also close the CD Writing Wizard before it completes.  In order to prevent this, we can create another loop that effectively waits until the wizard is no longer available, suggesting that it has closed after completion.

As you can see, although there is no direct method of writing files to a CD on Windows XP, WSH can be used to automate the built-in CD Writing Wizard to provide a scriptable alternative.  I hope that this example has shown you some of the flexibility of the WSH environment.  Now go out and find your own scripting workarounds.  Until next time, keep coding!

[gp-comments width="770" linklove="off" ]