Burning CDs in Windows XP with WSH - Building the Script
(Page 3 of 4 )
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.
Next: Finishing Up >>
More Windows Scripting Articles
More By Nilpo