Articles Archive
Articles Search
Director Wiki
 

Copying files off a CD-ROM

March 19, 2001
by Will Turnage

Dear Multimedia Handyman,

I want the user to select several files using a check box. When they finish there, I want to add a save button that lets them save the selected files somewhere on their hard drive. Also, all of the files to be copied will be on a CD so I need to allow for that too.

Thanks

Rojer

Dear Roger,

Your project basically requires you to do three different things. First, you need to determine what the path is to the user's CD-ROM drive. The next thing is to let the user select a folder where the files will be saved. Finally, you have to copy the files into that directory.

Unfortunately, Director doesn't have any of these capabilities built into it, but there are several Xtras available that provide part or all of this functionality for you. They include BuddyAPI, DirectOSXtra, and OSUtil Xtra. To work with the code examples in this column, though, you should use the new FileXtra3, which you can download from http://www.kblab.net/xtras/. This latest version of the FileXtra is newer than the one that ships free with Director. It's a little different from previous versions, so if you want to use the code samples in this movie, then you should download the new one. Best of all, it's free. Put the Xtra into the Xtras folder and restart Director to use it.

To use this Xtra in your project, the first step to take is to create an instance of the xtra.

fileXtraObj = new (xtra "filextra3")

Now that you've created an instance of the Xtra, find out the user's CD-ROM drive designation.

volumeList = fileXtraObj.fx_VolumesToList ()
cdRomVolume = ""
repeat with i in volumeList
  if fileXtraObj.fx_VolumeIsCDRom (i) then
    cdRomVolume = i
    exit repeat
  end if
end repeat

if cdRomVolume = "" then
  alert "Cannot find the CD"
  exit
end if

The first line of this code uses a handler called the FileXtra function fx_VolumesToList which returns a list of all volumes currently on your computer. Next, you initialize a variable called cdRomVolume and set its value equal to an empty string. After that, you use a repeat loop to go through each volume in the volumeList. For each volume, the code uses the fx_VolumeIsCDRom function to determine if the volume is a CD drive or not. If true, then it puts the path of that volume into the cdRomVolume variable and exits the repeat loop. Finally, it checks to see if the repeat loop found a CD-ROM drive or not. If it couldn't find a drive, it alerts the user and exits the handler.

The next step in your project is to let the user pick where the files will be copied.

if the platform contains "Windows" then
  destinationPath = fileXtraObj.fx_FolderSelectDialog ("Select a folder")
else
  destinationPath = fileXtraObj.fx_FolderSelectDialog (the moviePath)
end if

The first part of this code uses the fx_FolderSelectDialog handler to bring up a dialog box allowing the user to pick a directory where the files will be copied. For Windows, you can enter a string that will appear in the dialog box. For the Mac though, you have to specify the initial directory which the dialog box will display. To avoid errors, this example just uses the moviePath as the initial directory. The fx_folderSelectDialog function returns the path of the directory chosen by the user unless the user hits the cancel button, in which case the function returns an empty string. You might also want to test whether the chosen folder is on the CD-ROM volume.

The final step in your project is to copy the files from the CD to the hard drive.

if destinationPath <> "" then

  pictureList = ["movie.pic", "text.pic", "title.pic", "menus.pic"]

  repeat with i in pictureList
    flag = fileXtraObj.fx_fileCopy (cdRomVolume & i, destinationPath & i)
    if flag = 0 then
      alert "There was an error copying" && i
    end if
  end repeat

end if

fileXtraObj = 0

First you should check to make sure that the user actually picked a path to copy the files to and didn't just press the cancel button. If the user did choose a directory, then first you should create a list containing all of the names of your files. Once you have this list, all you need to do is use a repeat loop with that list. On each loop, the code will use the fx_fileCopy function along with the path to the CD and the path to the selected folder folder to copy the file from the CD to the hard drive. The fx_fileCopy function returns a 1 if the operation was successful and a 0 if the operation failed. If for some reason the file wasn't able to be copied, then the program alerts the user of the error.

Finally, when you're done using the Xtra, don't forget to dispose of the instance by setting it equal to zero. Failure to do so can result in memory leaks which might crash your project. Have fun coding.

Will Turnage is a multimedia programmer based in New York City. In addition to his weekly role as Director Online's Multimedia Handyman, he is also the Technology Director for Sony Music's Client Side Technologies Group. You can read more about his work at http://will.turnage.com/.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.