How can I Access a volume by name?

  1. 5 years ago

    Hello,

    This works on Mac...

    Dim DCIM As FolderItem
    Dim i as integer
    Dim DCIMExists as Boolean

    For i = 0 to VolumeCount-1
    if Volume(i).name = "DCIM" then
    DCIM = Volume(i)
    DCIMExists = True
    Exit
    end if
    next i

    If not DCIMExists then
    msgbox "The ""DCIM"" volume was not found."
    else
    msgbox "DCIM exists"
    end if

    How can I modify that for PC?

    Thanks.

    Lennox

  2. Garth H

    12 Jan 2015 Pre-Release Testers, Xojo Pro Answer

    As you know the Name property on Windows returns the drive letter, not the "volume name" which is more properly understood on Windows as a "drive label". This is important because the Name on the Mac is directly involved in the Path, whereas on Windows it's pretty much for the users convenience.

    The Windows Functionality Suite is your friend here, this code is from it, it's in the FileProcessing module.

    Protected Function GetVolumeName(root as FolderItem) As String
    #if TargetWin32
    Soft Declare Function GetVolumeInformationA Lib "Kernel32" ( root as CString, _
    volName as Ptr, volNameSize as Integer, ByRef volSer as Integer, ByRef _
    maxCompLength as Integer, ByRef sysFlags as Integer, sysName as Ptr, _
    sysNameSize as Integer ) as Boolean
    Soft Declare Function GetVolumeInformationW Lib "Kernel32" ( root as WString, _
    volName as Ptr, volNameSize as Integer, ByRef volSer as Integer, ByRef _
    maxCompLength as Integer, ByRef sysFlags as Integer, sysName as Ptr, _
    sysNameSize as Integer ) as Boolean


    dim volName as new MemoryBlock( 256 )
    dim sysName as new MemoryBlock( 256 )
    dim volSerial, maxCompLength, sysFlags as Integer

    if System.IsFunctionAvailable( "GetVolumeInformationW", "Kernel32" ) then
    Call GetVolumeInformationW( left( root.AbsolutePath, 3 ), volName, 256, volSerial, maxCompLength, _
    sysFlags, sysName, 256 )

    return volName.WString( 0 )
    else
    Call GetVolumeInformationA( left( root.AbsolutePath, 3 ), volName, 256, volSerial, maxCompLength, _
    sysFlags, sysName, 256 )

    return volName.CString( 0 )
    end if
    #endif
    End Function

  3. Thanks Garth,

    I got that and am trying to use it.

    OK, Let me explain a little more...

    I have a video processor connected to a PC. That videoprocessor has a PC Card Slot with a card inserted.

    What I want to do is to access images that are captured on that card.

    When I connect that video processor to the PC, the drive letter may vary depending on if there are other drives connected to the PC.

    So I would like to programatically identify the PC Card by name and not by letter.

    How can I do that?

    Thanks.

    Lennox

  4. Hi Garth,

    I am using this now...

    For i = 0 to VolumeCount-1
    if GetVolumeName(i).name = "DCIM" then
    DCIM = Volume(i)
    DCIMExists = True
    Exit
    end if
    next i

    but GetVolumeName needs more parameters

    What am I missing?

    Thanks.

    Lennox

  5. OK Garth,

    I got it now.

    Thanks.

    Lennox

  6. John H

    13 Jan 2015 Pre-Release Testers Planet earth

    You can also use WMI with OLEObject

    Here is an example to get all volumes:

      // http://library.wmifun.net/cimv2/win32_volume.html
      
      //http://msdn.microsoft.com/en-us/library/aa394515%28v=vs.85%29.aspx
      
      Dim locator, objWMIService, objs, objProperty  As OLEOBJECT
      Dim nobjs as Integer
      
      //  Connect to WMI
      locator = new oleObject("WbemScripting.SWbemlocator", true)
      
      Dim wmiServiceParams(2) as variant
      wmiServiceParams(1) = "."
      wmiServiceParams(2) = "root\cimv2"
      
      objWMIService= locator.invoke("ConnectServer", wmiServiceParams)
      
      // Run the WMI query
      objs = objWMIService.ExecQuery ("SELECT * FROM Win32_Volume")
      
      nobjs = objs.count - 1
      
      msgbox ("Number of Volumes: " + str(nobjs +1))
      
      For i as integer = 0 to nobjs
        Dim stringData As String
        
        objProperty = objs.ItemIndex(i)
        // ItemIndex() is not supported in Windows XP only from Windows Vista and upwards
        
        stringData = "Drive Letter: " + objProperty.Value("DriveLetter") + EndOfLine _
        + "Label: " +  objProperty.Value("Label") + EndOfLine _
        + "Description: " +  objProperty.Value("Description") + EndOfLine _
         +"Automount: "  + objProperty.Value("Automount") + EndOfLine _
        + "Block Size: " +  objProperty.Value("BlockSize") + EndOfLine _
        + "DeviceID: " + objProperty.Value("DeviceID") + EndOfLine _
        + "Capacity: " + objProperty.Value("Capacity") + EndOfLine _
        + "Caption: " + objProperty.Value("Caption") + EndOfLine _
        + "Compressed: " + objProperty.Value("Compressed") + EndOfLine _
        + "Device ID: " + objProperty.Value("DeviceID") + EndOfLine _
        + "Drive Type: " + objProperty.Value("DriveType") + EndOfLine _
        + "File System: " +  objProperty.Value("FileSystem") + EndOfLine _
        + "Free Space: " +  objProperty.Value("FreeSpace") + EndOfLine _
        + "Indexing Enabled: " +  objProperty.Value("IndexingEnabled") + EndOfLine _
        + "Maximum File Name Length: " + objProperty.Value("MaximumFileNameLength") + EndOfLine _
        + "Name: " + objProperty.Value("Name") + EndOfLine _
        + "Quotas Enabled: " + objProperty.Value("QuotasEnabled") + EndOfLine _
        + "Quotas Incomplete: " + objProperty.Value("QuotasIncomplete") + EndOfLine _
        + "Quotas Rebuilding: " + objProperty.Value("QuotasRebuilding") + EndOfLine _
        + "Serial Number: " + objProperty.Value("SerialNumber") + EndOfLine _
        + "SystemName: " + objProperty.Value("SystemName") + EndOfLine _
        + "Supports Disk Quotas: " + objProperty.Value("SupportsDiskQuotas") + EndOfLine _
        + "Supports File-Based Compression: " + objProperty.Value("SupportsFileBasedCompression") + EndOfLine
        
        msgbox stringData
      Next
      
      locator = Nil
      
    exception err as oleexception
      msgbox err.message

    Here is an example that display your drive letter from Volume Name:

      Sub ShowDriveLetter(VolumeName AS String)
      // http://library.wmifun.net/cimv2/win32_volume.html
      
      //http://msdn.microsoft.com/en-us/library/aa394515%28v=vs.85%29.aspx
      
      Dim locator, objWMIService, objs, objProperty  As OLEOBJECT
      Dim nobjs as Integer
      
      //  Connect to WMI
      locator = new oleObject("WbemScripting.SWbemlocator", true)
      
      Dim wmiServiceParams(2) as variant
      wmiServiceParams(1) = "."
      wmiServiceParams(2) = "root\cimv2"
      
      objWMIService= locator.invoke("ConnectServer", wmiServiceParams)
      
      // Run the WMI query
      objs = objWMIService.ExecQuery ("SELECT * FROM Win32_Volume where Label='"+VolumeName +"'")
      
      nobjs = objs.count - 1
      
      msgbox ("Number of Volumes: " + str(nobjs +1))
      
      For i as integer = 0 to nobjs
        Dim stringData As String
        
        objProperty = objs.ItemIndex(i)
        // ItemIndex() is not supported in Windows XP only from Windows Vista and upwards
        
        stringData = "Drive Letter: " + objProperty.Value("DriveLetter") + EndOfLine _
        + "Label: " +  objProperty.Value("Label") + EndOfLine _
        + "Caption: " + objProperty.Value("Caption") + EndOfLine
        
        msgbox stringData
      Next
      
      locator = Nil
      
    exception err as oleexception
      msgbox err.message
    End Sub
  7. Thanks John, quite a lot of info revealed.
    Lennox

  8. 2 months ago

    Dumb newbie question...

    How do I "install" the WFS so I can use the functions. New to Xojo and haven't worked with add-ons or plugins or whatever the WFS is...

    Thanks!

  9. Andrew L

    Aug 9 San Francisco, CA, USA

    WFS is a somewhat loose collection of self-contained source code snippets. You can (usually) just copy the methods/objects/modules that you need into your project. If you have the WFS project open in the Xojo IDE you can search for the GetVolumeName method and copy it from there, or you can copy lines 322-356 (inclusive) from the source file on Github

  10. Copy that. Appreciate the quick reply!!

or Sign Up to reply!