Burning Multisession CDs with IMAPI2 in WSH

In my last article, I showed you how you can use the Microsoft’s Image Mastering API (IMAPI2) control to create and burn single session CDs from a directory of files. Today, we’re going to take another look at the IMAPI2 control to create multisession CDs.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
November 10, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Traditional CD formats such as audio CDs and standard data CDs (like photo CDs, for example) are single session discs.  In other words, all of the information on the CD is written to the CD at one time in a single burning session.  Once completed, the disc is closed and its contents cannot be changed.

With the evolution of CD-Rewritable technology, things changed a bit.  Because you were now able to write and rewrite to the same disc, support was added that allowed a person to write to the same disc again in a new session, thus appending data to a disc that had already been written.  In other words, you could write data to the same disc across multiple sessions.

Of course, you would need a supporting drive to write the disc and you would also need a supporting drive to read it again.  However, almost all CD-RW capable drives are capable of reading and writing multisession discs.  That means that virtually every device currently on the market is capable.

In order to burn a multisession disc, each session must write its own lead-in and lead-out section on the disc.  The first session will require around 20MB of space to accomplish this and each subsequent session will need a little over 4MB or so.  This makes multisession discs slightly less efficient than single session.

This additional space is used to write information about the session, such as where the data starts, and how it fits into the disc's existing file system.  The lead-in section of the first session is slightly larger than the rest because it includes a Table Of Contents.  This TOC can be compared to a partition table on a hard drive.  It tells the CD-ROM what type of file system is in use and where the data resides on the disc.

Getting started

The process for burning multisession discs with IMAPI2 is virtually identical to that of burning single session discs.  We'll first set up the recording device, then prepare a data image, and finally, write that image to disc.  The process differs in how we create the disc image.

intIndex = 0

strPath = "C:BurnTemp"

 

Set objDiscMaster = WScript.CreateObject("IMAPI2.MsftDiscMaster2")

Just as before, we'll begin by telling our script to set a few variables.  We'll provide the index number of the drive we wish to use to burn our CD along with a path to the files that should be added.

We'll also create a reference to the IMAPI DiscMaster object.  This object returns a collection of all the optical devices on the system.

Set objRecorder = WScript.CreateObject("IMAPI2.MsftDiscRecorder2")

strDevice = objDiscMaster.Item(Index)

objRecorder.InitializeDiscRecorder(strDevice)

Next, we'll create a reference to the device we'll be using to record our CD.  The MsftDiscRecorder2 namespace returns an object that represents a recording device.  We'll then get a string that contains a unique ID for the device we wish to use.  Finally, we'll use the InitializeDiscRecorder method to bind the recorder object to the actual device on the system.

Set objImageWriter = CreateObject("IMAPI2.MsftDiscFormat2Data")

objImageWriter.Recorder = objRecorder

objImageWriter.ClientName = "Multisession CD"

Once a reference exists to a burning device, an ImageWriter object can be created to actually do the work.  The Recorder property is used to indicate what recording device to use, and the ClientName property is used to provide a friendly name.  This is the object that will be used to create an image of the current session and provide that to the recorder for writing.

Set objFSI = CreateObject("IMAPI2FS.MsftFileSystemImage")

At this point, set up of the disc and current recording session is complete.  It's now time to begin creating an image of the current burn session.

Building the burn image

In order to begin writing a new session to a disc, you must first check for any existing sessions on the disc to find where they end.  This is so the IMAPI2 control knows where to begin writing the new session on the disc.

If Not objImageWriter.MediaHeuristicallyBlank Then

The ImageWriter object provides the MediaHeuristicallyBlank method that returns a Boolean value indicating whether or not there are existing sessions on the disc in the drive.  You can create an If construct around this method to determine what course of action should be taken to create the current session image.  If this value is true, you should import the existing sessions and append to that file system.  If false, this indicates that we are writing the first session and we should create the file system.

   On Error Resume Next

   objFSI.MultisessionInterfaces = objImageWriter.MultisessionInterfaces

   If Err.Number <> 0 Then

       WScript.Echo "Multisession is not supported for this disc"

       WScript.Quit

   End If

   On Error GoTo 0

If a current session does in fact exist, we can perform a quick check to determine whether or not writing multiple sessions is supported by the current recording device.

   WScript.Echo "Importing data from the previous session..."

   objFSI.ImportFileSystem()

If at this point, all is well, it means that the disc contains existing sessions and that writing multiple sessions is supported.  We should then import the existing file system and append our new data to that.

Else

   objFSI.ChooseImageDefaults(objRecorder)

End If

If the disc in the drive is blank, we should create a new file system inside of our session image.  The ChooseImageDefaults does this easily by examining the drive and current media and creating a file system accordingly.  In most cases, the default ISO9660 format will be used.

Adding files and burning the image

At this point in our script, we have a session image to begin adding files to.  This image will contain any existing data on the disc.  Next, we'll add any files that we wish to burn, just as we did for single session discs.

WScript.Echo "Adding " & strPath & " directory to the disc..."

objFSI.Root.AddTree strPath, False

The AddTree method is used to add a directory structure and its contents to the existing disc image.  The first parameter is a string value indicating a path to a directory containing files to burn.  The second parameter is a Boolean value that indicates whether the parent folder should be included in the image or should only add its child objects.

Set objImage = objFSI.CreateResultImage()

Stream = objImage.ImageStream

Once all of the files have been added to the image, the CreateResultImage method is used to create the actual session image.  This is similar to saving an ISO image of a CD.  You must then create an IStream object to stream that image to the recording device.

objImageWriter.Write(Stream)

Once you have a valid image stream, the write process is started with a single call to the ImageWriter object's Write method.  You must provide the data stream to be written as a parameter.

Final thoughts

As you can see, it is very easy to write multisession discs using the IMAPI2 control.  This script can be run over and over again for as long as there is space remaining on the disc.

While writing multisession discs can be beneficial, there a few things to keep in mind. 

Once a session is written to disc, it is permanent.  This means that existing sessions remain on the disc regardless of what future sessions add.  This can lead to some unexpected results if you're not careful.

Let's take a look at an example.  If you were to begin a multisession disc by writing a 20MB file called Mydatabase.mdb to the root of a disc, then any time that you read that disc, you would see this file at the root of the disc in My Computer.

Now let's say that the contents of this file have changed and you want to update it on your disc.  The file now contains 25MB of data.  You can create a new session and write the updated file to the existing disc.

What results is nearly identical to the first disc.  In my computer you will still see a single file named Mydatabase.mdb at the root of your CD.  This will be the newly added 25MB version of the file.

As you add sessions to a disc, each session virtually overwrites any existing data on the disc if the same directory structure and file name exists.  You will only be able to see and access the most recent version of a file.  Thus, you can overwrite the existing file with an updated version when adding a new session.

However, as I stated earlier, sessions on disc are permanent.  This means that the original session and, in fact, the original file, still remain written on the disc, completely intact.  So while it appears that you have overwritten the original file, this is not true because you have not reclaimed any of the space that it consumed on disc.  In essence, your 25MB file consumes 45MB of disc space-the amount of space used each time the file is written.

So while you've accomplished the effort of updating a file on your disc by writing a new version of the file in a new session, you must keep in mind that you cannot reclaim the space used by the original file.  If you were updating an entire directory structure or very large files, disc space may very well become an issue.

That wraps up this article on writing multisession discs using the IMAPI2 control in WSH.  Go ahead and have some fun with it.  Until next time, keep coding!

blog comments powered by Disqus
WINDOWS SCRIPTING ARTICLES

- More Windows Scripting Workarounds from Nilpo
- Overloading Methods and More in VBScript
- Improving MFC for Windows Vista
- Regular Expressions in VBScript
- Working with Dates in WMI
- Completing Calendars with VBScript Date Func...
- Building Calendars with VBScript Date Functi...
- Working With Dates and Times in VBScript
- Designing WCF DataContract Classes Using the...
- Understanding Dates and Times in VBScript
- Working With Arrays in VBScript
- Compressed Folders in WSH
- Using .NET Interops in VBScript
- Nilpo`s Scripting Secrets, Vol I
- Database operations using Silverlight 2.0 WC...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials