Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


I need a script to change the CDROM drive letter

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2962
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> I need a script to change the CDROM drive letter
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 I need a script to change the CDROM drive letter - 5/11/2005 9:34:01 AM   
  lucdesaulniers

 

Posts: 10
Score: 0
Joined: 5/11/2005
From:
Status: offline
Hi,

I have a script that changes my cd-rom drive from d: to f: but it requires a reboot to be effective.

Does ne1 know if it is possible to make it effective right away without rebooting the computer?

Here is the script.

'Begining of script.
' Script that changes drive letters
' Note: Do NOT use it on SYSTEM or BOOT partition drive letters !!!
' Author: Torgeir Bakken


sComputer = "."
Const HKLM = &H80000002


' from/to
If ChangeDrvLetter("D:", "F:") Then
WScript.Echo "Drive letters changed, please reboot to see the change!"
Else
WScript.Echo "Failed changing drive letters!"
End If


Function ChangeDrvLetter(sSourceDrive, sTargetDrive)
bOK = True ' Init value
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& sComputer & "\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)


iRC = oReg.SetBinaryValue(HKLM, sKeyPath, sTrg, sValue)


If iRC = 0 Then
oReg.DeleteValue HKLM, sKeyPath, sSrc
Else
bOK = False
End If
Else
bOK = False
End If
ChangeDrvLetter = bOK
End Function
 
 
Post #: 1
 
 Re: I need a script to change the CDROM drive letter - 5/11/2005 3:10:25 PM   
  TNO


Posts: 1072
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Someone correct me if I'm wrong, but I think only Logical Drives can be renamed without having to reboot.

(in reply to lucdesaulniers)
 
 
Post #: 2
 
 Re: I need a script to change the CDROM drive letter - 5/12/2005 1:31:00 AM   
  lucdesaulniers

 

Posts: 10
Score: 0
Joined: 5/11/2005
From:
Status: offline
Why would we be able to change it through Disk management without reboot then?

There should be a way.

(in reply to lucdesaulniers)
 
 
Post #: 3
 
 Re: I need a script to change the CDROM drive letter - 5/12/2005 1:50:59 AM   
  TNO


Posts: 1072
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Maybe this will help:

http://support.microsoft.com/kb/300415/

(in reply to lucdesaulniers)
 
 
Post #: 4
 
 Re: I need a script to change the CDROM drive letter - 5/12/2005 2:49:23 AM   
  lucdesaulniers

 

Posts: 10
Score: 0
Joined: 5/11/2005
From:
Status: offline
Good to know but it does not fix my problem.
But thanks anyway for trying...

(in reply to lucdesaulniers)
 
 
Post #: 5
 
 Re: I need a script to change the CDROM drive letter - 5/12/2005 2:58:02 AM   
  TNO


Posts: 1072
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
"Why would we be able to change it through Disk management without reboot then?"

If that is the case could you not use the SendKeys Method to do it?

(in reply to lucdesaulniers)
 
 
Post #: 6
 
 Re: I need a script to change the CDROM drive letter - 5/12/2005 4:15:36 AM   
  lucdesaulniers

 

Posts: 10
Score: 0
Joined: 5/11/2005
From:
Status: offline
What do you mean?
Do you have an example?
Thanks.

(in reply to lucdesaulniers)
 
 
Post #: 7
 
 Re: I need a script to change the CDROM drive letter - 5/12/2005 9:28:29 AM   
  TNO


Posts: 1072
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Check here for info:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsmthsendkeys.asp

(in reply to lucdesaulniers)
 
 
Post #: 8
 
 Re: I need a script to change the CDROM drive letter - 5/13/2005 4:35:08 AM   
  Country73


Posts: 716
Score: 8
Joined: 8/25/2004
From: USA
Status: offline
I haven't had a chance to try this out, but maybe this link will help out:
http://groups.google.co.uk/group/microsoft.public.windowsxp.security_admin/msg/feed137416c78ee4?dmode=source

(in reply to lucdesaulniers)
 
 
Post #: 9
 
 Re: I need a script to change the CDROM drive letter - 5/13/2005 4:26:37 PM   
  TNO


Posts: 1072
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
That code is the first thing that was posted. It was already said that you still need to reboot.

(in reply to lucdesaulniers)
 
 
Post #: 10
 
 Re: I need a script to change the CDROM drive letter - 5/16/2005 2:39:04 AM   
  lucdesaulniers

 

Posts: 10
Score: 0
Joined: 5/11/2005
From:
Status: offline
Here is the new script I use and it works fine but I have an unknown problem with my MSI file that I launch this script with...When I execute the script directly by double-clicking on the vbs. It checks if the F G or H drive are free and if they are it reassign it to the CD drive. But when I launch the script through my MSI file, it maps the drive directly on the F drive and does not even see that it is already mapped.

Here is the vbs script :
' Changes the D drive to F,G or H drive depending if it is in use.
Dim nRtn, WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set oDrv = fso.Drives

If fso.DriveExists("F") = False Then
strRun = "diskpart /s c:\italk\scripts\chgdrvF.txt"
nRtn = wshShell.Run(strRun, 2 , True)
ElseIf fso.DriveExists("G") = False Then
strRun = "diskpart /s c:\italk\scripts\chgdrvG.txt"
nRtn = wshShell.Run(strRun, 2 , True)
ElseIf fso.DriveExists("H") = False Then
strRun = "diskpart /s c:\italk\scripts\chgdrvH.txt"
nRtn = wshShell.Run(strRun, 2 , True)
End If

***End of Script***
Here is the content of one of the chgDrv.txt that is a diskpart script :

select volume 0
assign letter=F
exit

***End Of Script***
If anyone have an idea to make this script work through an MSI it would be great. Thanks.

(in reply to lucdesaulniers)
 
 
Post #: 11
 
 RE: Re: I need a script to change the CDROM drive letter - 7/14/2007 9:24:53 AM   
  timdawg

 

Posts: 1
Score: 0
Joined: 7/14/2007
Status: offline
Excellent idea lucdesaulniers, however, I took it a little further.  To ensure that adding or changing devices doesn't mess with the volume numbers, I wrote a script that makes sure a specific Device ID gets a specific Drive Letter.  If the Drive letter is not correct, it writes the diskpart script, executes diskpart, then deletes the file it just created, and goes on to the next drive.  Here it is:

=====Script Start=====
On Error Resume Next

'Device IDs
DeviceID_V = ""
DeviceID_W = ""
DeviceID_X = ""
DeviceID_Y = ""
DeviceID_Z = ""

strComputer = "."
Dim nRtn, WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")

For Each objItem in colItems
Select Case (objItem.DeviceID)
   Case DeviceID_V
     x = ChgDrv ("V:", objItem.Drive)
   Case DeviceID_W
     x = ChgDrv ("W:", objItem.Drive)
   Case DeviceID_X
     x = ChgDrv ("X:", objItem.Drive)
   Case DeviceID_Y
     x = ChgDrv ("Y:", objItem.Drive)
   Case DeviceID_Z
     x = ChgDrv ("Z:", objItem.Drive)
  End Select
Next


Function ChgDrv(Drive, theDrive)
wscript.echo theDrive & " => " & Drive
If theDrive <> Drive Then

     'Create File
     strFilename = "C:\Scripts\cdrom\chgdrv" & Left(Drive,1) & ".txt"
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objFile = objFSO.CreateTextFile(strFilename)
     objFile.Close

     'Write Data to File
     Set objFile = objFSO.OpenTextFile (strFilename, 2)
     objFile.WriteLine "select volume " & theDrive
     objFile.WriteLine "assign letter=" & Drive
     objFile.WriteLine "exit"
     objFile.Close

     'Run Diskpart calling the script just created
     strRun = "diskpart /s " & strFilename
     nRtn = wshShell.Run(strRun, 2 , True)
    
     'Delete File
     objFSO.DeleteFile(strFilename)
End If
End Function
=====Script End=====
Don't forget to insert your Device IDs.

(in reply to lucdesaulniers)
 
 
Post #: 12
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> I need a script to change the CDROM drive letter Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts