|
-
Mar 16th, 2009, 09:15 AM
#1
Thread Starter
PowerPoster
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
-
Mar 16th, 2009, 10:16 AM
#2
Frenzied Member
Re: Determine if x partition is same physical drive as y partition
check out the GetVolumeInformation API
Good Luck
-
Mar 16th, 2009, 10:34 AM
#3
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:
'~~> Set reference to Microsoft Scripting Runtime 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
Usage
vb Code:
Sub HardDiskSerial() MsgBox GetDriveSerialNumber("C:") 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
-
Mar 16th, 2009, 11:03 AM
#4
Re: Determine if x partition is same physical drive as y partition
 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?
-
Mar 16th, 2009, 11:20 AM
#5
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
-
Mar 17th, 2009, 07:44 AM
#6
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
-
Mar 17th, 2009, 10:47 AM
#7
Thread Starter
PowerPoster
-
Mar 17th, 2009, 11:04 AM
#8
Re: Determine if x partition is same physical drive as y partition
 Originally Posted by cafeenman
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.
-
Mar 17th, 2009, 11:24 AM
#9
Frenzied Member
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
-
Mar 17th, 2009, 11:28 AM
#10
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.
-
Mar 18th, 2009, 12:11 PM
#11
Thread Starter
PowerPoster
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.
-
Apr 21st, 2009, 08:12 AM
#12
New Member
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!
-
Apr 22nd, 2009, 07:48 AM
#13
Thread Starter
PowerPoster
Re: Determine if x partition is same physical drive as y partition
 Originally Posted by steve6375
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.
-
Apr 22nd, 2009, 08:49 AM
#14
New Member
Re: Determine if x partition is same physical drive as y partition
-
Apr 22nd, 2009, 09:41 AM
#15
Thread Starter
PowerPoster
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!
-
Apr 22nd, 2009, 10:34 AM
#16
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|