How Can I Use a Script to Automatically Queue Up Files That I Want to Burn to a CD?


Hey, Scripting Guy! Question

Hey, Scripting Guy! In Windows XP, how can I use a script to automatically queue up files that I want to burn to a CD?

— BD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, BD. Well, unfortunately, we have some bad news for you: there’s currently no way to burn a CD using a script. You see, the problem is that – oh, wait a second: you just want to queue up the files that will be burned to the CD, don’t you? Well, that’s very different. And, for we Scripting Guys, a huge relief; after all, people ask us every other day about using scripts to burn CDs, and the answer is always the same: you can’t. That’s why we started this column off by telling you can’t burn a CD using a script; we’ve had to tell so many people that you can’t burn CDs using a script that this has become a reflexive reaction. But simply collecting all the files and getting them ready to be burnt to a CD? Well, that we can help you with.

Note. And for those of you who want to use scripts to burn CDs, well, we have at least some good news: this capability is coming in Windows Vista. In fact, in Windows Vista you’ll be able to use scripts to burn DVDs as well as CDs. Granted, this doesn’t help you too much today, but good things come to those who wait, right? You just need to wait a little bit longer and you’ll be able to burn all the CDs and DVDs you want.

As for queuing up files that will be burned to a CD, well, this little script should do the trick:

Const HKEY_CURRENT_USER = &H80000001

strComputer = “.”

Set objRegistry=GetObject(“winmgmts:\\” & strComputer & “\root\default:StdRegProv”)

strKeyPath = “Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders” strValueName = “CD Burning”

objRegistry.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strBurnFolder

strFolder = “C:\Test”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colFileList = objWMIService.ExecQuery _ (“ASSOCIATORS OF {Win32_Directory.Name='” & strFolder & “‘} Where ” _ & “ResultClass = CIM_DataFile”)

For Each objFile In colFileList strCopy = strBurnFolder & “\” & objFile.FileName & “.” & objFile.Extension objFile.Copy(strCopy) Next

There’s really no secret to queuing up files for burning: all you have to do is copy those files to the CD Burning folder (which will typically have a path like C:\Documents and Settings\kenmyer\Local Settings\Application Data\Microsoft\CD Burning). After you’ve determined the location of the CD Burning folder (something you can do by reading the registry) the rest is simply a matter of copying files to this folder.

With that in mind, let’s start with the first part of our task, determining the location of the CD Burning folder. In order to create a script that can queue up files on remote computers as well as on the local computer, we decided to use WMI to determine the folder location. To do that, we start out by defining a constant named HKEY_CURRENT_USER and setting the value to &H80000001; this tells the script which registry hive we want to work with. We then bind to the WMI service on the local computer (although, again, we could just as easily run this script against a remote machine), and connect to the System Registry provider. That’s what this code does:

Set objRegistry=GetObject(“winmgmts:\\” & strComputer & “\root\default:StdRegProv”)

Next we need to assign values to a pair of variables. The variable strKeyPath is assigned the value Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders; that just happens to be the path (within HKEY_CURRENT_USER) to the registry value we need to read. Meanwhile, the variable strValueName is assigned the value CD Burning; that just happens to be – oh, that’s right, that’s the name of the registry value we want to read.

Man, here we are trying to show off how much we know about scripting, only it turns out you guys know every bit as much as we do. Dang!

As you doubtless know then, we can now use the GetStringValue method to read the registry value and determine the path to the CD Burning folder:

objRegistry.GetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strBurnFolder

GetStringValue takes four parameters: the constant we defined at the beginning of our script, our two variables (strKeyPath and strValueName), and an “out” parameter named strBurnFolder. Being an out parameter we don’t assign a value to strBurnFolder; instead, the GetStringValue method will automatically assign the location of the CD Burning folder (as read from the registry) to that variable.

But you already knew that, didn’t you?

After we know the location of the CD Burning folder the next step is to copy files to that folder. For the sake of simplicity we’re going to assume that we want to copy all the files found in the folder C:\Test. To do that we first assign the path C:\Test to a variable named strFolder, then make a second connection to the WMI service, this time binding to the root\cimv2 namespace. After making that connection we can then use this query to return a collection of all the files found in the folder C:\Test:

Set colFileList = objWMIService.ExecQuery _
    (“ASSOCIATORS OF {Win32_Directory.Name='” & strFolder & “‘} Where ” _
        & “ResultClass = CIM_DataFile”)

At this point it gets just a tiny bit tricky. Why? Because copying files using WMI requires you to specify the complete path name of the copied file. For example, suppose we want to copy the file C:\Test\File1.txt. The only way we can copy this file is to provide WMI with the full path to the new file: C:\Documents and Settings\kenmyer\Local Settings\Application Data\Microsoft\CD Burning\File1.txt. Like we said, that problem is a tiny bit tricky, but it’s one we can solve easily enough. And here’s how.

To begin with, we set up a For Each loop to loop through all the files in the folder C:\Test. Inside that loop we start off by using this line of code to construct the path for the copied file:

strCopy = strBurnFolder & “\” & objFile.FileName & “.” & objFile.Extension

You can see what we’re doing here. We start off by taking the location of the CD Burning folder (represented by the variable strBurnFolder) and then add a trailing \ to the folder name. We then tack on the value of the FileName property (for example, File1) followed by a dot (.) and the value of the Extension property (for example, txt). After all that the variable strCopy will contain a value similar to this:

C:\Documents and Settings\kenmyer\Local Settings\Application Data\Microsoft\CD Burning\File1.txt

Which, by remarkable coincidence, just happens to be the path to the copied file.

Once we have that path we can then pass the value to the Copy method and copy the file to the CD Burning folder:


From there we loop around and repeat this process for the next file in the collection. After we’ve looped through the entire collection all the files in C:\Test will have been copied to the CD Burning folder, and you’ll be ready to burn those files to a CD.

Or wait until Windows Vista is released and then use a script to burn those files to a CD. We’ll leave that up to you. And, yes, you can bet that we’ll fill you in on how to use scripts to burn CDs and DVDs in Windows Vista; trust us, after years of delivering people the same bad news (“No, sorry, you can’t use a script to burn a CD”) we can’t wait to give people a better answer. At long last we’ll have an answer for every question you guys can throw at us.

What’s that? You want to know how manage a DHCP server using a script? OK, maybe not every question ….


Discussion is closed.

Feedback usabilla icon