
Set/Change CD-ROM drive letter with VBS
Rolf,
This is a section from my RIS script
Regards
John Facci
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Change CD Rom Drive Letter to R:
' Requires reboot before it takes effect.
' Auto registers "RegObj.Dll" 'This will have been copied to System32 dir
during RIS install.
' Enumerates all mounted devices
' saves key name of last CD found
' ensures R: not already used
' Created new key for CD with letter of R:
' Deleted old Key.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub CDROM
On Error Resume Next
Dim objRegistry, objRootKey, objRegKey
Dim objDevice
Dim strDevice, strDriveLetter, d
Dim strFound ' Store Name of device to rename
Dim strTemp, strNewKey
Dim strReg
Dim f, result
Set objRegistry = WScript.CreateObject("RegObj.Registry")
If err.number <> 0 then 'DLL must not be registered
RegDLL "Regobj.dll" 'Attempt Automatic Registration.
Err.Clear
' Try creating object again
Set objRegistry = WScript.CreateObject("RegObj.Registry")
if err.number <> 0 then 'DLL STILL not registered.
ErrHandler "Error: Auto registration of DLL failed"
End IF
End if
strFound = ""
Set objRootKey =
objRegistry.RegKeyFromString("\HKEY_LOCAL_MACHINE\System\MountedDevices")
' Loop through all Devices
For Each objDevice In objRootKey.Values
strDevice = objDevice.Name
' Is this a Device we are interested in
If Len(strDevice)=14 Then
If Left(strDevice,12) = "\DosDevices\" Then
' Extract Drive Letter
strDriveLetter = Right(strDevice,2)
If strDriveLetter = "R:" Then 'Letter already in use
LogMsg "Unable to rename CDROM as R: already in use"
Exit Sub
End if
If strDriveLetter = "A:" or strDriveLetter = "B:" or
strDriveLetter = "C:" Then
' Do nothing as we are not interested in A:, B: or C:
Else
' Check to see if the drive is a CDROM
Set d = fso.GetDrive(strDriveLetter)
If d.DriveType = 4 Then
strFound = strDevice 'Save Device name for later
usage.
End if
End if
End if
End if
Next 'objDevice
Set objRootKey = Nothing
If strFound = "" Then
LogMsg "No CDROM found on this PC"
Exit Sub
End if
' We get here if we found a CDROM device to rename
' Read its Value (Binary) as we need value to create a new Key based on
R:
' We must use RegObj to read the Key as the Value contains a "\" which is
not supported by RegRead
strTemp =""
' The registry key we are creating in a Binary Array, VBScript only
supports an array variant
' this is not good enough for RegObj.DLL
' In order to create the new key we must create a temporary .REG file and
import it.
' Read current Value, this is a VarType of 8209 - Binary Array
strTemp = RegRead("\HKEY_LOCAL_MACHINE\System\MountedDevices" ,
strFound)
BugCheck "Error: CDROM() - Error Reading Value: " & strFound
' Convert Byte Array to Hex Character String
strTemp = ByteArray(strTemp)
' We must create a string to write to the Temp.REG file
strReg = "Windows Registry Editor Version 5.00" & vbCRLF & vbCRLF
strReg = strReg & "[HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices]" & vbCRLF &
vbCRLF
strReg = strReg & Chr(34) & "\"
' Add Key name to Value
strTemp = "\DosDevices\\R:" & chr(34) & "=hex:" & strTemp
' Split The Binary data into Lines 75 chars long (looks better in the
.Reg file
Do While Len(strTemp) > 75
strReg = strReg & Left(strTemp,75) & "\" & vbcRLF & " "
strTemp = Right(strTemp,Len(strTemp)-75)
Loop
' Add remaining Array characters (there may have been less that 75 on the
last line
strReg = strReg & strTemp & vbCRLF
' Create .REG File in Temp directory
Set f = fso.OpenTextFile(WshSysEnv("TEMP") & "\RIS.REG", ForWriting,
True)
f.WriteLine strReg
f.close
BugCheck "Error: CDROM() - Error creating .REG file.'"
' Import Registry settings
result = WshShell.Run ("Regedit.exe /S " & WshSysEnv("TEMP") &
"\RIS.REG", 1, True)
' Ensure new registry entry is present, before deleting the old one.
strTemp = RegRead("\HKEY_LOCAL_MACHINE\System\MountedDevices", strFound)
BugCheck "Error: CDROM() - Error Reading Value: " & strFound
' Read New Value
strReg = RegRead("\HKEY_LOCAL_MACHINE\System\MountedDevices",
Left(strFound,Len(strFound)-2) & "R:")
BugCheck "Error: CDROM() - Error Reading Value: " &
Left(strFound,Len(strFound)-2) & "R:"
If Ubound(strReg) <> UBound(strTemp) Then
ErrHandler "CDROM() - New Registry value dosn't match old one"
' This is a issue as we now have a cactus registry entry for R:
End if
' Delete old Key
'WshShell.RegDelete("HKEY_LOCAL_MACHINE\System\MountedDevices\" &
strFound)
' Get the key containing the value we want to remove
Set objRootKey =
objRegistry.RegKeyFromString("\HKEY_LOCAL_MACHINE\System\MountedDevices\")
' Remove the Value from the Values collection
objRootKey.Values.Remove (strFound)
BugCheck "Error: CDROM() - Error deleting existing key"
' Delete temp .REG file
fso.DeleteFile(WshSysEnv("TEMP") & "\RIS.REG")
BugCheck "Error: CDROM() - Error deleting temp .REG file: " &
WshSysEnv("TEMP") & "\RIS.REG"
LogMsg "Assigned R: to CDROM, reboot for this to take effect"
On Error Goto 0
End Sub
Quote:
> Hi,
> is it possible to change the CD-ROM drive letter with VBS? During our
> automated installation will want to change the drive letter of the CD-ROM
to
> "z:"...
> Thanks for help in advance