Results 1 to 16 of 16

Thread: Determine if x partition is same physical drive as y partition

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Determine if x partition is same physical drive as y partition

    I have been putting backup routines in my projects to save whatever file is important to the application.

    If the user selects to backup to the same drive as the original file then I warn him that it's a bad idea.

    But that doesn't work if he chooses to backup to a different partition on the same physical hard drive.

    So I want to warn if backing up to the same physical drive.

    Answer I need:

    How do I determine what physical drive a partition is part of?

    Code:
    Public Function BackupProjectFile() As Long
    Dim sMsg As String
    Dim sBackupFilename As String
    Dim nReturn As Long
    Dim FSO As New FileSystemObject
    Dim sCurrentDrive As String
    Dim sBackupDrive As String
    
    On Error GoTo errHandler
    ' Returns Error Code.
    
    sMsg = "In the event of Drive failure, both the original file and the backup file may be lost." & vbCrLf & "Continue with backup operation?" & DBL_RETURN
    sMsg = sMsg & "Click Yes to continue or No to select a different path to backup your Project."
    
    Top:
    
    If sBackupFilename = vbNullString Then sBackupFilename = ProjectFilename
    
    sBackupFilename = GetFileName(FILE_SAVE, sBackupFilename, 0, WEBMASTER_Project_v2_FILE_FILTERS, BackupPath(sBackupFilename), vbNullString, "wpj2")
    
    If sBackupFilename = vbNullString Then Exit Function
    
    sCurrentDrive = FSO.GetDriveName(ProjectFilename)
    sBackupDrive = FSO.GetDriveName(sBackupFilename)
    
    If sCurrentDrive = sBackupDrive Then
      nReturn = MsgBox(sMsg, vbYesNoCancel + vbQuestion, APP_TITLE)
      Select Case nReturn
        Case Is = vbYes
          ' Do nothing
        Case Is = vbNo
          GoTo Top
        Case Is = vbCancel
          Exit Function
      End Select
    End If
    
    nReturn = BackupFile(ProjectFilename, sBackupFilename)
    If nReturn <> 0 Then Err.Raise nReturn
    
    MsgBox ProjectFilename & DBL_RETURN & "successfully backed up to" & DBL_RETURN & sBackupFilename & ".", vbInformation, APP_TITLE
    
    Registry.SaveSetting "StartUp", "Backup Path", GetFolder(sBackupFilename)
    
    Set FSO = Nothing
    
    ' Return 0
    
    Exit Function
    
    errHandler:
    Dim nErrReturn As Long
    Dim nErr As Long
    
    nErr = Err
    nErrReturn = ErrorHandler(Error, nErr, vbNullString, "bWebmasterUtilties2.BackupProjectFile()")
    
    If nErrReturn = vbRetry Then Resume
    
    BackupProjectFile = nErr
    
    Set FSO = Nothing
    
    End Function

  2. #2
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Determine if x partition is same physical drive as y partition

    check out the GetVolumeInformation API

    Good Luck

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Determine if x partition is same physical drive as y partition

    One more way to do this...

    Remember no matter how many partitions are there... there will only be one Hard Disk Serial number. Hope this helps...

    vb Code:
    1. '~~> Set reference to Microsoft Scripting Runtime
    2. Public Function GetDriveSerialNumber(Optional ByVal DriveLetter As String) As Long
    3.     Dim fso As Object, Drv As Object
    4.     '~~> Create a FileSystemObject object
    5.     Set fso = CreateObject("Scripting.FileSystemObject")
    6.          
    7.     '~~> Assign the current drive letter if not specified
    8.     If DriveLetter <> "" Then
    9.         Set Drv = fso.GetDrive(DriveLetter)
    10.     Else
    11.         Set Drv = fso.GetDrive(fso.GetDriveName(App.Path))
    12.     End If
    13.      
    14.     With Drv
    15.         If .IsReady Then
    16.             DriveSerial = Abs(.SerialNumber)
    17.         Else    '~~> "Drive Not Ready!"
    18.             DriveSerial = -1
    19.         End If
    20.     End With
    21.          
    22.     '~~> Clean up
    23.     Set Drv = Nothing
    24.     Set fso = Nothing
    25.     GetDriveSerialNumber = DriveSerial
    26. End Function

    Usage

    vb Code:
    1. Sub HardDiskSerial()
    2.     MsgBox GetDriveSerialNumber("C:")
    3. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Determine if x partition is same physical drive as y partition

    Quote Originally Posted by koolsid
    One more way to do this...

    Remember no matter how many partitions are there... there will only be one Hard Disk Serial number. Hope this helps...
    Hmmm, I have 3 SATA hard drives, each drive has four partitions, and each drive letter returned a different number using that code, I also tried an API way using GetVolumeInformation and that too returns a different number for each drive letter.

    Maybe these solutions only work on PATA drives?

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Determine if x partition is same physical drive as y partition

    Found this, seems to work OK for me.
    Code:
    Private Sub Command2_Click()
        Dim wmiDiskDrive As Object
        Dim wmiDiskDrives As Object
        Dim wmiDiskPartition As Object
        Dim wmiDiskPartitions As Object
        Dim wmiLogicalDisk As Object
        Dim wmiLogicalDisks As Object
        Dim wmiServices As Object
        Dim wmiQuery As String
        
        ' Get physical disk drive Letter
        Set wmiServices = GetObject( _
            "winmgmts:{impersonationLevel=Impersonate}!//" & ".")
        
        Set wmiDiskDrives = wmiServices.ExecQuery("SELECT Caption, DeviceID FROM Win32_DiskDrive")
        
        For Each wmiDiskDrive In wmiDiskDrives
            Debug.Print "Disk drive Caption: " _
                & wmiDiskDrive.Caption _
                & vbNewLine & "DeviceID: " _
                & " (" & wmiDiskDrive.DeviceID & ")"
            wmiQuery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
                & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
            Set wmiDiskPartitions = wmiServices.ExecQuery(wmiQuery)
        
            For Each wmiDiskPartition In wmiDiskPartitions
                'Use partition device id to find logical disk
                Set wmiLogicalDisks = wmiServices.ExecQuery _
                    ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
                     & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
        
                For Each wmiLogicalDisk In wmiLogicalDisks
                    Debug.Print "Drive letter associated" _
                        & " with disk drive = " _
                        & wmiDiskDrive.Caption _
                        & wmiDiskDrive.DeviceID _
                        & vbNewLine & " Partition = " _
                        & wmiDiskPartition.DeviceID _
                        & vbNewLine & " is " _
                        & wmiLogicalDisk.DeviceID
                Next
            Next
        Next
    End Sub

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Determine if x partition is same physical drive as y partition

    @Edge: Yeah, you are right. The code for drive serial number which I gave above will change after every format... Thats a neat code but will not give you the unique Serial number....

    Here is one link which is useful...

    http://www.planet-source-code.com/vb...57366&lngWId=1
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: Determine if x partition is same physical drive as y partition

    Thanks for the replies. I have e-mail notification enabled but I wasn't notified so I thought there were no responses.

    I thought of using the Serial number approach but while searching the googles for clues, I found several sources that said it's not reliable because some drives don't have serial numbers although I've never come across one.

    Also, it doesn't matter if the serial number changes during a format because this is a "now" thing.

    In other words it's going to compare the drive the current file is on to the drive the User selects to back up the file to. He's not going to have time to format the drive between the time he selects the file on the drive about to be formatted and saving the backup.

    I'll try the serial number approach. It's not a huge big deal that I even accomplish this. I just want to do my part to annoy users with lots of warning pop-ups.

    I want to be my own Vista.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Determine if x partition is same physical drive as y partition

    Quote Originally Posted by cafeenman View Post
    Thanks for the replies. I have e-mail notification enabled but I wasn't notified so I thought there were no responses.
    That would be due to your email provider I'm afraid - for some reason notification emails get stopped by some providers (yet not every time ), and despite the obvious opt-in we can't persuade them to unblock us.

    Apparently the best way to deal with it is to create a GMail account to get the notifications, and set it up to auto-forward to your real address.

  9. #9
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Determine if x partition is same physical drive as y partition

    Or some sites will allow you to recieve the email if you add the senders address to your contact list

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Determine if x partition is same physical drive as y partition

    Unfortunately based on what many people have said, that doesn't work - the emails get deleted before they reach that stage.

  11. #11

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: Determine if x partition is same physical drive as y partition

    Serial number doesn't work. Gave me two different serials for two partitions on the same drive.

  12. #12
    New Member
    Join Date
    Mar 2007
    Posts
    11

    Re: Determine if x partition is same physical drive as y partition

    See the post (resolved) to get a physicial drive number given a drive letter. This will solve your problem. I would avoid using wmi for this as in my experience it can occasionally hang using the Associate on disk volumes!

  13. #13

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: Determine if x partition is same physical drive as y partition

    Quote Originally Posted by steve6375 View Post
    See the post (resolved) to get a physicial drive number given a drive letter. This will solve your problem. I would avoid using wmi for this as in my experience it can occasionally hang using the Associate on disk volumes!
    Can you please link to the thread or provide the title? I couldn't find the thread you're talking about.

    Thanks.

  14. #14
    New Member
    Join Date
    Mar 2007
    Posts
    11

    Re: Determine if x partition is same physical drive as y partition


  15. #15

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: Determine if x partition is same physical drive as y partition

    That mostly does it. It doesn't work for my RAID drives though. My D: drive is a pair of mirrored 500 GB drives and the function returns 0,0,0 for drive type, drive number and partition number. For all other drives including optical drives it works.

    I have to see how it works on a network drive which I don't have available at the moment.

    Thanks!

  16. #16
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Determine if x partition is same physical drive as y partition

    I found this code at http://www.vbgold.com/vb-projects/di...l-number.shtml
    that looks similar to the code in one of the post above.
    Code:
          Private Sub Form_Load()
          
              'Show drive serial number for the current drive
              MsgBox " Drive serial number for " & Left(App.Path, 1) & ": " & GetDriveSerialNumber
              End
          
          End Sub
          --------------------------------------------------------------------------------
          Public Function GetDriveSerialNumber(Optional ByVal DriveLetter As String) As Long
          
              Dim fso As Object, Drv As Object
              
              'Create a FileSystemObject object
              Set fso = CreateObject("Scripting.FileSystemObject")
              
              'Assign the current drive letter if not specified
              If DriveLetter <> "" Then
                  Set Drv = fso.GetDrive(DriveLetter)
              Else
                  Set Drv = fso.GetDrive(fso.GetDriveName(App.Path))
              End If
          
              With Drv
                  If .IsReady Then
                      DriveSerial = Abs(.SerialNumber)
                  Else    '"Drive Not Ready!"
                      DriveSerial = -1
                  End If
              End With
              
              'Clean up
              Set Drv = Nothing
              Set fso = Nothing
              
              GetDriveSerialNumber = DriveSerial
              
          End Function
    fso.drv.SerialNumber return the Serial Number of the Volume (partition) not of the physical hard disk.
    As mentioned above, Volume Serial Number can be changed on format, 2 partitions of the same disk usually will have 2 different Serial Numbers.

    This code won't help but simpler than above:
    Code:
    Sub MyHardDiskVolumeSerialNumbers()
       Dim fso As Object '-- Scripting.FileSystemObject
       Dim dr As Object '-- Scripting.Drive
       
       '-- Set fso = New Scripting.FileSystemObject
       Set fso = CreateObject("Scripting.FileSystemObject")
       For Each dr In fso.Drives
          If dr.DriveType = Fixed Then
             Debug.Print dr.DriveLetter & ": " & dr.SerialNumber
          End If
       Next
       Set dr = Nothing
       Set fso = Nothing
    End Sub
    I didn't play with this project http://www.a1vbcode.com/app-4080.asp
    but perhaps it works for IDE drives only.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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