Finding USB volumes on Win

Richard Gaskin ambassador at fourthworld.com
Tue Mar 23 09:13:58 EDT 2010


Kay C Lan wrote:

> On Mon, Mar 22, 2010 at 10:05 PM, Richard Gaskin wrote:
>> In January with the help of Phil, Peter, and others I got some great tips
>> for identifying removable drives on OS X and Linux.  But I'm having
>> difficulty figuring out how to do the same in Windows.
>
> Richard,
>
> back then you wrote:
>
> In the meantime, it looks like I have some parsing to do and some homework
>> to figure out the details of getting this info for Win Vista, Win 7, and
>> Linux.  I'll post the result here once I get it working.
>
> I'm hoping that offer still applies?

Absolutely.  This has been a group effort, with valuable input from Ken 
Ray, Phil Davis, Peter Alcibiades, Matthias Rebbe, and others, so the 
least I can do is share the fruit of our collective labor.

Below is a handler that handles Mac and Win, with a placeholder for 
Linux but I don't need that yet so I'll write it in a few weeks when I 
get to it.

It examines the currently-mounted volumes and returns a list of those 
volumes which are removable, using AppleScript for the Mac side and data 
from the Win registry for Windows.

Each line of the return value has three items separated by tabs:

- Drive label/letter (I use this to display in a list)
- available space (useful for my installer)
- mount point or drive letter (needed for writing to the drive)

I have four different removable drives I've been testing with (card 
reader, U3, plain USB flash drive, and camera), and the AppleScript 
method that was suggested here was the only one which easily recognized 
my card reader along with all the other more "normal" drives; relying on 
info from system_profiler alone did not provide the mount point, needed 
to write files to the drive.

Note that U3 drives show up as two volumes, one writable and one not. 
The non-writable volume of a U3 drive has zero available space, and 
that's used to exclude them from the list without having to slow things 
down by attempting writes to each disk to test that (though also do that 
on the selected volume later on in a different handler just to make sure 
before proceeding with the install).

This is a bit sloppy in some parts since it's a small part of a larger 
project in which there's a LOT of work do to, but it seems to get the 
job done well enough for now (you may need to clean up email-imposed 
line wraps):



function RemovableDrives
   -- Returns a tab-delimited list of drives which are removable
   -- and have a file system usable by the current OS
   -- in which each line is:
   -- <Drive name/letter> <tab> <free space> <tab> <mountpoint/drive letter>
   --
   put the volumes into tVols
   put empty into tRemVols
   put the directory into tSaveDir
   switch the platform
   case "MacOS"
     repeat for each line tVol in tVols
       put "tell application "&quote&"Finder"&quote&cr&\
           "properties of disk "&quote& tVol &quote &cr&\
           "end tell" into tAS
       do tAS as applescript
       put the result into tInfo
       if (", ejectable:true," is in tInfo) \
           AND (", free space:0, " is not in tInfo) then
         put offset(", URL:"&quote&"file://", tInfo) into tOS
         delete char 1 to (tOS+13) of tInfo
         set the itemdel to "/"
         delete item 1 of tInfo
         put "/"&word 1 of tInfo into tMountPoint
         delete char -3 to -1 of tMountPoint
         set the directory to tMountPoint
         if the result is not empty then next repeat
         put the diskspace into tSpace
         put tVol &tab& tSpace &tab&  tMountPoint &cr after tRemVols
       end if
     end repeat
     break
   case "Win32"
     set cursor to watch
     put the hideConsoleWindows into tSaveHCW
     set the hideConsoleWindows to true
     -- Hex for "\??\STORAGE#RemovableMedia#":
     put 
"5C003F003F005C00530054004F0052004100470045002300520065006D006F00760061006200"&\
         "6C0065004D00650064006900610023" into tRemovableIdentifier
     repeat for each line tVol in tVols
       if tVol is in "A:B:C:" then next repeat -- two floppies and the 
main HD
       put "reg query HKLM\SYSTEM\MountedDevices /v \DosDevices\"& tVol 
into tCmd
       put shell(tCmd) into tmp
       if tRemovableIdentifier is in line -2 of tmp then
         set the directory to tVol
         if the result is empty then
           put "Removable Disk ("&tVol&")" &tab& the diskspace & tab& 
tVol &cr after tRemVols
         end if
       end if
     end repeat
     set the directory to tSaveDir
     set the hideConsoleWindows to tSaveHCW
     break
   case "Linux"
     -- add here -- http://linux.die.net/man/8/blkid -- thanks Peter!

     break
   end switch
   --
   delete last char of tRemVols -- trailing CR
   return tRemVols
end RemovableDrives


If you find any bugs with this please let me know.  I'll be putting it 
into testing with my own users soon, but it would of course be good to 
iron out as many issues as possible in advance.  Thanks -

--
  Richard Gaskin
  Fourth World
  Rev training and consulting: http://www.fourthworld.com
  Webzine for Rev developers: http://www.revjournal.com
  revJournal blog: http://revjournal.com/blog.irv



More information about the use-livecode mailing list