Results 1 to 21 of 21

Thread: [RESOLVED] How to identify a specific disk, media or flash memory

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    762

    Resolved [RESOLVED] How to identify a specific disk, media or flash memory

    I am developing an application to back up my files.
    The computer has two internal hard drives C and D.
    Also, I sometimes add one or two usb flash memories. That would be either E (when I add only one usb flash memory) or E and F (when I add two usb flash memories)
    The destination of backup is usually (but not always) an external hard drive, but I cannot know if that external hard drive is F or G, because that depends on how many usb flash memories I connect to the computer.
    Therefore, each time I run this application, I should manually check what drive symbol (F or G) has been assigned to the external hard drive, and then I specify that in a textbox on my application.
    This is not good.
    What further complicates the matter is that when I install this application on another computer, the destination of backup would be another usb flash memory instead of the external hard disk.
    I need to implement a mechanism that would automatically identify the disk or media that is the destination of the backup.
    I should somehow mark, tag or specify the external hard disk only once, and then the application should be smart enough to look for that mark or tag.

    The method that comes to my mind is to create a dummy file by a very specific name on the disk that is supposed to be the destination, on the root folder.
    For example I can create an empty file named "MyBackupDiskNo1.txt"
    Then my program can loop through the following and see which one of these files actually exists:
    D:\MyBackupDiskNo1.txt
    E:\MyBackupDiskNo1.txt
    F:\MyBackupDiskNo1.txt
    G:\MyBackupDiskNo1.txt

    Z:\MyBackupDiskNo1.txt
    And the one that exists, identifies the disk.

    This technique works, but it looks a little awkward to me.
    Can anybody propose a better, more professional technique?
    Thanks.

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: How to identify a specific disk, media or flash memory

    Do you enter parameters into your program before you start the search? If so you could probably the name, etc of the actual drive, etc to make sure it meets your requirements before preforming the requested operation.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    762

    Re: How to identify a specific disk, media or flash memory

    No, I don't enter the parameters.
    The application is smart enough to figure out everything.
    That is why I am looking for a way to code into the application to make it figure out the destination disk.
    Please advise.
    Thanks.

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: How to identify a specific disk, media or flash memory

    Code:
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colDisks = objWMIService.ExecQuery _
        ("Select * from Win32_LogicalDisk")
    For Each objDisk in colDisks
        Wscript.Echo "DeviceID: "& vbTab &  objDisk.DeviceID       
        Select Case objDisk.DriveType
            Case 1
                Wscript.Echo "No root directory. Drive type could not be " _
                    & "determined."
            Case 2
                Wscript.Echo "DriveType: "& vbTab &  "Removable drive."
            Case 3
                Wscript.Echo "DriveType: "& vbTab &  "Local hard disk."
            Case 4
                Wscript.Echo "DriveType: "& vbTab &  "Network disk."      
            Case 5
                Wscript.Echo "DriveType: "& vbTab &  "Compact disk."      
            Case 6
                Wscript.Echo "DriveType: "& vbTab &  "RAM disk."   
            Case Else
                Wscript.Echo "Drive type could not be determined."
        End Select
    Next
    More here
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    762

    Re: How to identify a specific disk, media or flash memory

    Thanks for the code.
    However, if there are three USB flash memories, and only one of them is the destination for the backup, how do I programmically identify which one?
    Thanks.

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: How to identify a specific disk, media or flash memory

    Quote Originally Posted by IliaPreston View Post
    Thanks for the code.
    However, if there are three USB flash memories, and only one of them is the destination for the backup, how do I programmically identify which one?
    Thanks.
    How do you current tell the computer which drive to backup to?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    762

    Re: How to identify a specific disk, media or flash memory

    I have created an empty file named "MyBackupDiskNo1.txt" on the root folder on that disk.
    Then my program loops through the following and checks to see which one of these files actually exists:

    D:\MyBackupDiskNo1.txt
    E:\MyBackupDiskNo1.txt
    F:\MyBackupDiskNo1.txt
    G:\MyBackupDiskNo1.txt
    ...
    Z:\MyBackupDiskNo1.txt
    And the one that exists, identifies the disk.
    This method works.
    But, I think this is a bit awkward.
    I am wondering if there is a better way, a more professional way of doing it.
    Please advise.
    Thanks.

  8. #8
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: How to identify a specific disk, media or flash memory

    Have you considered making use of the volume label?

    You can give any drive a unique label by simply renaming it through Windows Explorer. Then you can figure out its drive letter using code like this:

    Code:
    'Retrieves the drive letter of a volume given its label.
    'Returns an empty string if no such volume label exists.
    'Do not pass an empty string to VolumeLabel!
    
    Public Function GetDriveLetterOf(ByRef VolumeLabel As String) As String
        Dim lCharCode As Long
    
        GetDriveLetterOf = " :\"
    
        For lCharCode = vbKeyD To vbKeyZ
            Mid$(GetDriveLetterOf, 1&) = ChrW$(lCharCode)
            If StrComp(Dir(GetDriveLetterOf, vbVolume), VolumeLabel, vbTextCompare) = 0 Then Exit Function
        Next
    
        GetDriveLetterOf = vbNullString  'No match found
    End Function
    Sample usage:

    Code:
    MsgBox GetDriveLetterOf("MyBackupDiskNo1"), vbInformation
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  9. #9
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    Re: How to identify a specific disk, media or flash memory

    Every hard drive, and most any other removable media drive disks, including Flash Drives, when formatted are assigned a unique serial number (Long). You only have to identify this number once and log this number with the method of your choice. On subsequent usage, you can identify any specific drive with that serial number.

    I don't have the information in front of me (I am on my laptop atm) but look up for the API call : GetVolumeInformationA on MSDN for the details and the proper Declare statement.

    Edit : You can also poll for drive existence with this routine. Say you call for drive letter F to I, drives with no flash drive inserted will return 0, else they will return the serial number.
    Last edited by Navion; Oct 20th, 2014 at 05:30 AM.

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,855

    Re: How to identify a specific disk, media or flash memory

    Yeah, I was thinking the same thing. Or possibly name the drive's volume, allowing backups to only specifically named volumes. I don't have those API calls in front of me either, but they're fairly straightforward to use. And yep, the GetVolumeInformationA that Navion mentioned is gonna get it done.

    Code at http://www.ex-designz.net/apidetail.asp?api_id=601 appears to do it.

    EDIT: I actually tested it for grins, and it's a pretty nice piece of code. Here it is:

    Code:
    Private Declare Function GetVolumeInformation Lib "Kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long
    
    Private Sub Form_Load()
    Dim Serial As Long, VName As String, FSName As String
    'Create buffers
    VName = String$(255, Chr$(0))
    FSName = String$(255, Chr$(0))
    'Get the volume information
    GetVolumeInformation "C:\\", VName, 255, Serial, 0, 0, FSName, 255
    'Strip the extra chr$(0)'s
    VName = Left$(VName, InStr(1, VName, Chr$(0)) - 1)
    FSName = Left$(FSName, InStr(1, FSName, Chr$(0)) - 1)
    MsgBox "The Volume name of C:\\ is '" + VName + "', the File system name of C:\\ is '" + FSName + "' and the serial number of C:\\ is '" + Trim(Str$(Serial)) + "'", vbInformation + vbOKOnly, App.Title
    End Sub

  11. #11
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: How to identify a specific disk, media or flash memory

    Elroy,
    and if we replace 'Trim(Str$(Serial)) ' with Hex$(Serial) in that example we end up with something which echoes the serial number reported when we enter Vol Drive from a command/ cmd prompt; albeit without the '-' delimiter after the 4th character.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    762

    Re: How to identify a specific disk, media or flash memory

    Thanks a lot for all your help.
    Before I set this thread to resoled, I have a few questions:
    1. 1. Can volume name and file system names be unicode (for example Chinese, Russian, Greek, etc.)? and in that case will the function GetVolumeInformation retrieve them correctly?
    2. 2. I used the code provided by Elroy to retrieve and display the VName, FSName and Serial No of all my disks. The serial numbers that it returned for all but one of them are positive numbers, but the serial number that it returned for one of them (Drive D) is a negative number (approximately negative 990 million and something). Is that right? Looks strange to me.
    3. 3. When in Windows explorer I right-click on any drive and check its properties, I can see that drives volume name, anf file system name. So, I can easily verify the correctness of the code that Elroy provided. However, that properties page, doesn't show the serial number. So, How can I verify the above code as far as serial number is concerned? How can I see the serial number?


    Please advise.
    Thanks.

  13. #13
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: How to identify a specific disk, media or flash memory

    >How can I verify the above code as far as serial number is concerned?

    >...when we enter Vol Drive from a command/ cmd prompt

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    762

    Re: How to identify a specific disk, media or flash memory

    Thank you for the response.
    Can volume name and file system names be unicode? and in that case will the function GetVolumeInformation retrieve them correctly?
    Can the serial number be negative?
    Thanks.

  15. #15
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: How to identify a specific disk, media or flash memory

    >Can the serial number be negative?
    Yes which is probably why they are usually reported in Hex format; to do that we replace 'Trim(Str$(Serial)) ' with Hex$(Serial) in the example as previously advised.

    >Can volume name and file system names be unicode?
    Very likely because http://msdn.microsoft.com/en-gb/libr...(v=vs.85).aspx mentions two versions of the API call namely GetVolumeInformationW (Unicode) and GetVolumeInformationA (ANSI)

  16. #16
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,855

    Re: How to identify a specific disk, media or flash memory

    Here you go. I fixed the snippet I posted earlier to work with the ...W function. I tested it and it seems to work just fine. I left the ASCII declaration in, but it's not used.

    Code:
    Private Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long
    Private Declare Function GetVolumeInformationW Lib "kernel32" (ByVal lpRootPathName As Long, ByVal lpVolumeNameBuffer As Long, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As Long, ByVal nFileSystemNameSize As Long) As Long
    Private Sub Form_Load()
        Dim Serial As Long, VName As String, FSName As String
        'Create buffers
        VName = String$(255, Chr$(0))
        FSName = String$(255, Chr$(0))
        'Get the volume information
        GetVolumeInformationW StrPtr("C:\\"), StrPtr(VName), 255, Serial, 0, 0, StrPtr(FSName), 255
        'Strip the extra chr$(0)'s
        VName = Left$(VName, InStr(1, VName, Chr$(0)) - 1)
        FSName = Left$(FSName, InStr(1, FSName, Chr$(0)) - 1)
        MsgBox "The Volume name of C:\\ is '" + VName + "', the File system name of C:\\ is '" + FSName + "' and the serial number of C:\\ is '" + Trim(Str$(Serial)) + "'", vbInformation + vbOKOnly, App.Title
    End Sub
    EDIT: And yeah, a Hex$(Serial) rather than that Trim(Str$(Serial)) may be a bit cleaner. I didn't patch that up.

    EDIT2: You know? Staring at that thing, I see another bug that may come up in the rarest of circumstances. Please change the two String$(255, chr$(0)) to String$(256, chr$(0)). In the event that Windows fills the buffer, you'll still have a chr$(0) for the trim functions to work.
    Last edited by Elroy; Oct 26th, 2014 at 10:04 AM.

  17. #17
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: How to identify a specific disk, media or flash memory

    Its no the way to find the letter of drive to backup a problem. How and when you can extract that data... is more serious...because...if you attach the backup in line...then how can you save it if a mains overcharge and blow every pc in line? So you have to think to back up to somewhere isolated from the main system. And for that matter, no need to automatic check where you back up...You just complete a form.
    As for a joke...this is a story only...
    I think an automatic system to backup need a "spy" as a program to run without UI and an attach usb key, without notice by the laptop owner...and voila...we have the data...on the stick..Except if we don't get the right info from vbforums and suddenly we save the data to the SD card...that the owner forgot to tell us...

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    762

    Re: How to identify a specific disk, media or flash memory

    Thank you everyone for the great responses.
    As for the code that Elroy provided, I think if I change the two String$(255, chr$(0)) to String$(256, chr$(0)), then I also have to change this line:
    Code:
    GetVolumeInformationW StrPtr("C:\\"), StrPtr(VName), 255, Serial, 0, 0, StrPtr(FSName), 255
    to this:
    Code:
    GetVolumeInformationW StrPtr("C:\\"), StrPtr(VName), 256, Serial, 0, 0, StrPtr(FSName), 256
    Am I right?

  19. #19
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to identify a specific disk, media or flash memory

    Using "C:\\" is a silly but non-fatal error. Only one "\" is used, e.g. "C:\" here.

    I'd guess some cargo-cult coder was copying an example written in C, where a "\" is escaped as "\\" in a string literal.

    Despite claims earlier in this thread volume serial numbers are NOT unique. This is a soft value and users can change it. Indeed, preformatted media (flash drive cards, USB flash drives) often arrive with every one of them set to 0 (0000-0000).

    In the end you might just as well use a file like you started with. Alternatively you might consider hardware serial values but these are not as easily obtained and the methods required vary by both type of media and adapter (ATAPI/IDE, SATA, SCSI, USB, etc.) used.

  20. #20
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: How to identify a specific disk, media or flash memory

    Quote Originally Posted by IliaPreston View Post
    I need to implement a mechanism that would automatically identify the disk or media that is the destination of the backup.
    I should somehow mark, tag or specify the external hard disk only once, and then the application should be smart enough to look for that mark or tag.
    So you need some sort of device ID? Of the alternatives to the dummy file presented so far (volume label/name, volume serial number, hardware serial value), the volume label/name is IMO the easiest one to set and retrieve. Setting it is as easy as renaming the backup drive using Windows Explorer, while retrieving it can be done via the intrinsic Dir() function (see this example). Even though Dir() isn't Unicode-aware, it isn't much of a handicap if all you want is just a unique identifier. According to MSDN, "the maximum volume label length is 32 characters" (just 11 characters though for FAT file systems ). This limitation is luckily sufficient enough to allow the use of a GUID in text notation (sans the hyphens). For your convenience, here's a GUID generator you can paste and run in the Immediate Window:

    Code:
    ? Replace(Mid$(CreateObject("Scriptlet.TypeLib").GUID, 2&, 36&), "-", vbNullString)
    You're free, of course, to compose your own unique volume label (especially in the case of FAT file systems).
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    762

    Re: How to identify a specific disk, media or flash memory

    Thanks a lot everyone for all your wonderful help and advice.
    I am going to set this thread to Resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width