How can I Access a volume by name?

  1. ‹ Older
  2. 5 years ago

    John H

    13 Jan 2015 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
  3. Thanks John, quite a lot of info revealed.
    Lennox

  4. 10 months ago

    Andy B

    9 Aug 2019 Airdrie, AB, Canada

    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!

  5. Andrew L

    9 Aug 2019 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

  6. Andy B

    9 Aug 2019 Airdrie, AB, Canada

    Copy that. Appreciate the quick reply!!

  7. 5 days ago

    Lennox J

    May 21 Jamaica. West Indies
    Edited 5 days ago

    Still having difficulties... I had given up on it, but really need it now.

    What is meant by "root as FolderItem" in Protected Function GetVolumeName(root as FolderItem) As String?

    I have a SanDisk Ultra USB 3.0 USB flash drive, it is listed as "LSJ files (G)" but that G can change if another usb is inserted or if used in another computer.

    How do I use "root as FolderItem? I am using Xojo 2015r4.1

    Thanks.

    Lennox

  8. Emile S

    May 21 Europe (France, Strasbourg)

    Try http://docs.xojo.com/FolderItem.DriveAt instead of root.

  9. Lennox J

    May 21 Jamaica. West Indies

    Thanks Emile,

    I should have mentioned that I am using Xojo 2015r4.1

    Thanks again.

    Lennox

  10. Julian S

    May 21 Xojo Pro UK

    Here you go Lennox, I think this is what you're looking for?

    https://www.dropbox.com/s/iknztsi45xdgvew/TestVolumeEnumeration.xojo_binary_project?dl=0

    Calls used FindFirstVolumeW, FindNextVolumeW, FindVolumeClose, GetVolumePathNamesForVolumeNameW, GetVolumeInformationW (added here for to aid future searches)

  11. Lennox J

    May 21 Jamaica. West Indies

    Hi Julian,

    Thanks, I am using Xojo 2015r4.1, maybe it is too old for this.
    I ran and also compiled it without any errors.
    What I got is a window named Untitled with two menus... File and Edit.
    File has a menuitem named Exit.
    The window itself is blank.

    I will download a newer version and see if there is a difference.

    Thanks again.

    Lennox

  12. @Lennox J :
    add a textarea to the window
    change the code in the open event to:

    Dim paths() As String = GetPathByVolumeName("")
    
    For Each path As String In paths()
      system.DebugLog("path=" + path)
      TextArea1.AppendText "path=" + path + EndOfLine
    Next

    IMHO you should see some more in the window.

    Personally i find it easier to show test data in a textarea instead of the systemlog as Julian does, but that's just me..! But as you can see Julian is the brilliant genius!

  13. Emile S

    May 22 Europe (France, Strasbourg)

    @Lennox J Xojo 2015r4.1

    http://docs.xojo.com/Volume

  14. Julian S

    May 22 Xojo Pro UK

    @Emile S http://docs.xojo.com/Volume

    Emile, thanks for the suggestion, but you can't easily get the volume name using that, so you'd still need to dip into Declares.

    @Lennox J The window itself is blank.

    Lennox, if you run the program, then click this tab at the bottom of the IDE window, you'll see the output:

    -image-

    This is the Messages Panel, it can be used for debugging and outputting information at runtime, it's very handy for simple things like this https://docs.xojo.com/UserGuide:Find,_Errors,_Messages_Panels#Messages

  15. 4 days ago

    Emile S

    May 22 Europe (France, Strasbourg)

    Hi Julian,

    my answer comes from:

    @Lennox J What is meant by "root as FolderItem" in Protected Function GetVolumeName(root as FolderItem) As String?

    @Lennox J I should have mentioned that I am using Xojo 2015r4.1

    @Emile S http://docs.xojo.com/Volume

    A second read leads to understand Lennox already knows about Volume (earlier in the thread, in 2015).

  16. Lennox J

    May 22 Jamaica. West Indies
    Edited 4 days ago

    OK Thanks.

    Julian, thanks for that , I did not know about that, but all it gives is this...
    my Application.exe launched
    path=H:\

    Andre, I got this...
    https://ibb.co/1n2wzGr

    Emile, I did not get it to work, I gave up.

    Thanks again.

    Lennox

  17. Julian S

    May 22 Xojo Pro UK

    Lennox, I'm afraid I can't write your program for you. I've given you a function that will return a path for a given volume name, it returns H:\ because that is the drive that is where "LSJ FILES" is mounted in the image you linked above. If that volume ends up on another drive letter, that letter will be returned. Isn't that what you wanted?

  18. Lennox J

    May 22 Jamaica. West Indies

    Hi Julian,

    Thanks, but I did not want you to write the program for me.

    What I wanted was to know "How do I use "root as FolderItem?" as mentioned above, but you went ahead and provided some code which I was not familiar with, I did not even know what that debugger thing was for. Thanks for your efforts still, I learnt something in the process.

    So the question remains How do I use "root as FolderItem in Protected Function GetVolumeName(root as FolderItem) As String?

    No sweat. Thanks again.

    Lennox

  19. Julian S

    May 22 Xojo Pro UK

    I should have read the post a bit closer instead of answering the original question. Taken from the code you posted in 2015.

    Dim i As Integer
    Dim s As String
    For i = 0 To VolumeCount - 1
      s = GetVolumeName(Volume(i))
      system.DebugLog(s)
    Next i
  20. 3 days ago

    Lennox J

    May 23 Jamaica. West Indies

    Fantastic!!! Thanks Julian.
    Works just as I wanted.
    Thanks again.
    Lennox

  21. Emile S

    May 23 Europe (France, Strasbourg)

    @Julian S Volume

    Sic !

or Sign Up to reply!