jamesagmoore Posted September 4, 2005 Share Posted September 4, 2005 HelloI've created a script to change drive letters based on the volume label (from My Computer) or the CD rom device name (which can be seen in device manager). It uses WMI to query to CD drive descriptions and partition labels.So for example I can setup the PC like this:cscript SetDriveLetterFromDescription.vbs //Nologo "HL-DT-ST DVD-ROM GDR8163B" "Y:"cscript SetDriveLetterFromDescription.vbs //Nologo "HL-DT-ST DVDRAM GSA-4163B" "Z:"cscript SetDriveLetterFromDescription.vbs //Nologo "Data" "D:"cscript SetDriveLetterFromDescription.vbs //Nologo "512MB DISK" "U:"cscript SetDriveLetterFromDescription.vbs //Nologo "80Gb USB2 Disk" "P:"following a reboot the drive letters in explorer will change.Shortcomings:1) No support for multiple installations of same type of CDROM or same volume name in a single PC2) No neat way of handling removable media (eg, compact flash)3) No protection from people breaking their Windows install by reassigning the boot or system driveSave the below code in a file named "SetDriveLetterFromDescription.vbs". Take care before running it.Const ComputerName = "."Const HKLM = &H80000002Set wmiServices = GetObject ( "winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName)if WScript.Arguments.count <> 2 then WScript.Echo "Usage: " & WScript.ScriptName & " Description DriveLetter" WScript.Quit 1end ifSetDriveLetterFromDescription WScript.Arguments(0), WScript.Arguments(1) function SetDriveLetterFromDescription(strDescription,strNewLetter) currentdriveletter = GetDriveLetterFromDescription(strDescription) if currentdriveletter = vbNullString then strMessage = " Unknown description" elseif UCase(currentdriveletter)=UCase(strNewLetter) then strMessage = " Already set" elseif ChangeDrvLetter(currentdriveletter,strNewLetter) then strMessage = " OK" else strMessage = " Failed" end if WScript.Echo strDescription & " " & currentdriveletter & " -> " & strNewLetter & strMessageend functionfunction GetDriveLetterFromDescription(strDescription) Set wmiDiskDrives = wmiServices.ExecQuery("SELECT Caption,Description,DeviceID,Drive FROM Win32_CDROMDrive WHERE Caption =""" & strDescription & """") For Each wmiDiskDrive In wmiDiskDrives GetDriveLetterFromDescription = wmiDiskDrive.Drive exit function next Set wmiDiskDrives = wmiServices.ExecQuery ( "SELECT Caption FROM Win32_LogicalDisk WHERE VolumeName =""" & strDescription & """") For Each wmiDiskDrive In wmiDiskDrives GetDriveLetterFromDescription = wmiDiskDrive.Caption exit function next end functionFunction ChangeDrvLetter(sSourceDrive, sTargetDrive) bOK = True ' Init value Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & ComputerName & "\root\default:StdRegProv") sKeyPath = "SYSTEM\MountedDevices" sSrc = "\DosDevices\" & UCase(sSourceDrive) iRC = oReg.GetBinaryValue(HKLM, sKeyPath, sSrc, sValue) If iRC = 0 Then sTrg = "\DosDevices\" & UCase(sTargetDrive) oReg.DeleteValue HKLM, sKeyPath, sSrc iRC = oReg.SetBinaryValue(HKLM, sKeyPath, sTrg, sValue) If iRC <> 0 Then bOK = False End If Else bOK = False End If ChangeDrvLetter = bOKEnd Function Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now