Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

drop drive letter if exist and attach logical drive to drive letter

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I'm having problems with some USB drives, I need to set up 5 USB drives for our backup.

I need them to all be drive letter F:, 4 drives when unplugged and plugged back in re-assign correctly, as they are identical USB drives.

However western digital seem incapable of sending us the right drive and we have one that is 320gb instead of 250gb.

I decided to try to write a script which would disconnect any drive using letter F: and reassign it, but I'm having trouble...

here is my script
Code:
Option Explicit

Dim strComputer, objWMIService, colItems, volumeItem, objItem

'On Error Resume Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_Volume Where Name = 'E:\\'")
For Each objItem in colItems
    objItem.Dismount True, True 
Next

' define available drives
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE VolumeName = 'Monday' OR VolumeName='Tuesday' OR VolumeName='Wednesday' OR VolumeName='Thursday' OR VolumeName='Friday'",,48)

For Each volumeItem in colItems
    volumeItem.DriveLetter = "F:"
    volumeItem.Put_
Next

set objWMIService = nothing
set colItems = nothing
set volumeItem = nothing
set objItem = nothing

I'm not getting any errors, but it's not working either.

I have found when going into disk management and manualy assigning the drive letter, the volume name keeps being changed from 'Monday' to 'Local Disk', why is this happening?

can anyone advise how I resolve this issue?

Thanks,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
It's OK, I've changed my mind. This has been a royal pain in the proverbial!

I even allowed the drive to be a different letter and used...
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colVolumes = objWMIService.ExecQuery _
    ("Select * from Win32_Volume Where Name = 'G:\\'")
For Each objVolume in colVolumes
    objVolume.DriveLetter = "F:"
    objVolume.Put_
Next

Which worked until the other 4 drives that were happy to be drive letter F: were plugged back in then they lost their volume label and drive letter.

In the end I just reconfigured my File System Device in the backup software so one drive is letter G: and the rest are on letter F:

Otherwise the drives kept loosing their volume names and not being assigned a drive letter.

This way it now seems to work.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top