Tracy7011 Posted December 2, 2005 Posted December 2, 2005 Thanks,But how would I use this program to rename the drive letter of the cd rom to R:, how would I use this to tell me what letter the cdrom is and to remove that letter and assign R:?Thanks in advance!
Sonic Posted December 2, 2005 Posted December 2, 2005 test.exe doesn't do anything with drive letter ! It detects just what type of drives and if the condition is right it launch a program.
gunsmokingman Posted December 2, 2005 Posted December 2, 2005 (edited) Here is a VBS script that search for CD and checks if It a RW then it alllow you to change the drive letter.strComputer = "."On Error Resume NextDim Act, CD,ChangeDrv,Fso,MyPos,SearchString,SearchChar,SDSet Act = CreateObject("Wscript.Shell")Set Fso = CreateObject("Scripting.FileSystemObject")SD = Act.ExpandEnvironmentStrings("%Systemdrive%")Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")'''' GET THE CDROM DRIVE LETTER'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Function CD_Get_Drive For Each objItem in colItems SearchString = objItem.Caption MyPos = Instr(1, SearchString, "RW") CD = objItem.Drive If MyPos = 1 Then MsgBox "This Is Not A CD-RW", 0 + 32, "Not A CD-RW" Exit Function Else SearchChar = "Caption: " & objItem.Caption & vbCrLf & "Name: " & objItem.Name & vbCrLf & CD Act.Popup SearchChar, 5, "Confirm CD-RW",0 + 32 Change_CD_Letter '''''PLACE THE SOFTWARE INSTALL PART BELOW HERE ''Act.Run(ChangeDrv & "\THE_FOLDER\THE_APP SWITCHES_FOR_INSTALL),1,True '''''END THE SOFTWARE INSTALL HERE Exit For End If Next End Function'''' CHANGE DRIVE LETTER HERE''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function Change_CD_Letter Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colVolumes = objWMIService.ExecQuery("Select * from Win32_Volume Where Name = '" & CD & "\\'") For Each objVolume in colVolumes ChangeDrv = InputBox("Please type in the new drive letter you would like to assign to the drive" &_ vbCrLf & "The current Drive Letter Is : " & CD & vbcrlf & "Example Z: or Y:","Change Drive" ) objVolume.DriveLetter = "" & ChangeDrv & "" objVolume.Put_ Next Exit Function End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''CD_Get_DriveAct.Popup "Completed CD Drive Letter Change" &_vbCrLf & "Old CD Drive Letter : " & CD &_vbCrLf & "New CD Drive Letter : " & ChangeDrv, 7,"Finished", 0 + 32If Fso.FileExists(SD & "\CD_ChangeLetter.vbs") Then Fso.DeleteFile(SD & "\CD_ChangeLetter.vbs")End If Edited December 3, 2005 by gunsmokingman
Tracy7011 Posted December 3, 2005 Posted December 3, 2005 I like the script but when I run it,I get a confirm on the: "Act.Popup SearchChar, 5, "Confirm CD-RW",0 + 32", it goes away or I can click "Ok".Then I get the: "Act.Popup "Completed CD Drive Letter Change" &_vbCrLf & "Old CD Drive Letter : " & CD &_vbCrLf & "New CD Drive Letter : " & ChangeDrv, 7,"Finished", 0 + 32"It seems to skip the whole Function Change_CD_Letter The new drive letter text is "" (blank) and it doesn't change anything on the drive. Any ideas?
gunsmokingman Posted December 3, 2005 Posted December 3, 2005 I made that script on Vista and it worked but on XP it will not changed the drive letter.I will try and re-edit so it works on XP later.
Tracy7011 Posted December 3, 2005 Posted December 3, 2005 Thanks for your help! I need this for Win2000, however I can't get it to work on either.Thanks again!
Tracy7011 Posted December 3, 2005 Posted December 3, 2005 (edited) Thanks for your help, I got it figured out. I use 3 .vbs scripts 2 .bat files and 2 diskpart.exe script text files to remove the drive letter from the cd-rom and assign it the letter r:, find out what disk (0) or (1) the 4G volume c: is on and create another partition with the letter e: out the remaining space and format it.I am sure there is a prettier and easier way but this is the first time I have used scripts. I got all the code straight from the Microsoft script site and cut and paste as required.What I am doing:I call the scripts in this order from GuiRunOnce***********Script 1: attrib.vbs************************************************************'modifies the attributes of two diskpart script files called "disk0.txt" and "disk1.txt" to make sure they' are not readonly.Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.GetFile("C:\install\disk0.txt")If objFile.Attributes AND 1 ThenobjFile.Attributes = objFile.Attributes AND 0' Wscript.Echo "Read-only."End If Set objFile = objFSO.GetFile("C:\install\disk1.txt")If objFile.Attributes AND 1 ThenobjFile.Attributes = objFile.Attributes AND 0' Wscript.Echo "Read-only."End If **********Script 2: drive.vbs*************************************************************'1. Finds Volume Letter of CD-Rom'2. Modifies a "diskpart.exe" script file called disk0.txt and another disk1.txt to include the command' "select volume" and the returned drive letter in line 1. (example: "select volume D:")Const FOR_READING = 1Const FOR_WRITING = 2strFileName0 = "C:\install\disk0.txt"strFileName1 = "C:\install\disk1.txt"strNewContent = "select volume "strComputer = "."Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2")Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk")For Each objDisk in colDisks' Wscript.Echo "DeviceID: "& vbTab _' & objDisk.DeviceID Select Case objDisk.DriveType Case 1 Case 2 Case 3 Case 4 Case 5 'Write it to the First File "disk0.txt" Set objFS = CreateObject("Scripting.FileSystemObject") Set objTS = objFS.OpenTextFile(strFileName0,FOR_READING) strContents = objTS.ReadAll objTS.Close Set objTS = objFS.OpenTextFile(strFileName0,FOR_WRITING) 'this next line write the string into the first line of the text file objTS.WriteLine strNewContent & objDisk.DeviceID objTS.Write strContents 'Write to the Second File "disk1.txt" Set objFS = CreateObject("Scripting.FileSystemObject") Set objTS = objFS.OpenTextFile(strFileName1,FOR_READING) strContents = objTS.ReadAll objTS.Close Set objTS = objFS.OpenTextFile(strFileName1,FOR_WRITING) 'this next line write the string into the first line of the text file objTS.WriteLine strNewContent & objDisk.DeviceID objTS.Write strContents Case 6 Case Else End SelectNext**********Script 3: drivetopart.vbs*********************************************************'Finds the disk id (Disk 0 or Disk 1) of C: drive and launches the correct correct bat file so I can create 'E: drive out of the remaining space.'Tells us which letter goes with what partition..ComputerName = "."Set wmiServices = GetObject ( _ "winmgmts:{impersonationLevel=Impersonate}!//" _ & ComputerName)' Get physical disk driveSet wmiDiskDrives = wmiServices.ExecQuery ( _ "SELECT Caption, DeviceID FROM Win32_DiskDrive")For Each wmiDiskDrive In wmiDiskDrives 'Use the disk drive device id to ' find associated partition query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _ & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition" Set wmiDiskPartitions = wmiServices.ExecQuery(query) 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 wmiLogicalDisksif wmiDiskDrive.DeviceID = "\\.\PHYSICALDRIVE0" then Set objShell = WScript.CreateObject("WScript.Shell") objShell.Run("%comspec% /K c:\install\edrive0.bat"), 1, Trueend ifif wmiDiskDrive.DeviceID = "\\.\PHYSICALDRIVE1" then Set objShell = WScript.CreateObject("WScript.Shell") objShell.Run("%comspec% /K c:\install\edrive1.bat"), 1, Trueend if Next NextNext##################The other files#########################********* edrive0.bat%windir%\system32\diskpart.exe /S c:\install\disk0.txt > c:\install\drivelog.txtecho Y | %windir%\system32\format e: /fs:ntfs /q /v:""*********edrive1.bat*****************%windir%\system32\diskpart.exe /S c:\install\disk1.txt > c:\install\drivelog.txtecho Y | %windir%\system32\format e: /fs:ntfs /q /v:""*********drive0.txt 'this is a diskpart script file*********************'select volume e: This is what drive.vbs will write ERASE THIS LINE IF YOU ARE USING THIS!!!remove noerrassign letter rSelect Disk 0 Create partition primaryassign letter eexit*********drive1.txt 'this is a diskpart script file*********************'select volume e: This is what drive.vbs will write ERASE THIS LINE IF YOU ARE USING THIS!!!remove noerrassign letter rSelect Disk 1 Create partition primaryassign letter eexitI hope this helps someone, I know it is dirty code and I am not sure how to post it in the pretty quote box.Like I said I cut and paste from the Microsoft site. I know it works for my app!Thanks again Edited December 3, 2005 by Tracy7011
gunsmokingman Posted December 3, 2005 Posted December 3, 2005 Here is the 3VBS scripts as one VBS script try this and see if it works stillConst FOR_READING = 1, FOR_WRITING = 2ComputerName = "."strComputer = "."Dim MyFile '''' REPLACES THESE VARIBLES NAMES ,strFileName0,strFileName1,strNewContentMyFile = Array("C:\install\disk0.txt","C:\install\disk1.txt","select volume")Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.GetFile(MyFile(0))If objFile.Attributes AND 1 ThenobjFile.Attributes = objFile.Attributes AND 0End If Set objFile = objFSO.GetFile(MyFile(1)) ''''USINF THE ARRAY VARIBLE HERE NOWIf objFile.Attributes AND 1 ThenobjFile.Attributes = objFile.Attributes AND 0End If Set objWMIService = GetObject("winmgmts:" _& "{impersonationLevel=impersonate}!\\" _& strComputer & "\root\cimv2")Set colDisks = objWMIService.ExecQuery _("Select * from Win32_LogicalDisk")For Each objDisk in colDisksSelect Case objDisk.DriveTypeCase 1Case 2Case 3Case 4Case 5Set objFS = CreateObject("Scripting.FileSystemObject")Set objTS = objFS.OpenTextFile(MyFile(0),FOR_READING)strContents = objTS.ReadAllobjTS.CloseSet objTS = objFS.OpenTextFile(MyFile(0),FOR_WRITING)objTS.WriteLine MyFile(2) & objDisk.DeviceIDobjTS.Write strContentsobjTS.CloseSet objTS = objFS.OpenTextFile(MyFile(1),FOR_READING)strContents = objTS.ReadAllobjTS.CloseSet objTS = objFS.OpenTextFile(MyFile(1),FOR_WRITING)objTS.WriteLine MyFile(2) & objDisk.DeviceIDobjTS.Write strContentsobjTS.CloseCase 6Case ElseEnd SelectNextSet wmiServices = GetObject ( _"winmgmts:{impersonationLevel=Impersonate}!//" _& ComputerName)Set wmiDiskDrives = wmiServices.ExecQuery ( _"SELECT Caption, DeviceID FROM Win32_DiskDrive")For Each wmiDiskDrive In wmiDiskDrivesquery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _& wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition" Set wmiDiskPartitions = wmiServices.ExecQuery(query)For Each wmiDiskPartition In wmiDiskPartitionsSet wmiLogicalDisks = wmiServices.ExecQuery _("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _& wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") For Each wmiLogicalDisk In wmiLogicalDisks If wmiDiskDrive.DeviceID = "\\.\PHYSICALDRIVE0" then Set objShell = WScript.CreateObject("WScript.Shell") objShell.Run("%comspec% /K c:\install\edrive0.bat"), 1, True End If If wmiDiskDrive.DeviceID = "\\.\PHYSICALDRIVE1" then Set objShell = WScript.CreateObject("WScript.Shell") objShell.Run("%comspec% /K c:\install\edrive1.bat"), 1, True End If Next NextNext
Tracy7011 Posted December 3, 2005 Posted December 3, 2005 (edited) Thanks!That works except there is a space after the word volume in "select volume" --> "select volume "ChangeMyFile = Array("C:\install\disk0.txt","C:\install\disk1.txt","select volume")ToMyFile = Array("C:\install\disk0.txt","C:\install\disk1.txt","select volume ")After I added the space it worked great.Thanks alot, I will have to figure out the variable array.... Edited December 3, 2005 by Tracy7011
gunsmokingman Posted December 3, 2005 Posted December 3, 2005 I took out that space I did not know if it was needed.Just add it to the array with the space at the end.This has the space added to the end.MyFile = Array("C:\install\disk0.txt","C:\install\disk1.txt","select volume ")
Tracy7011 Posted December 3, 2005 Posted December 3, 2005 Yep, that does it. Thanks for making them one!
gunsmokingman Posted December 3, 2005 Posted December 3, 2005 Your welcome and if you need help just message me
Tracy7011 Posted December 3, 2005 Posted December 3, 2005 (edited) Ok, I am done with this now.This script will:1. find your cd-rom drive letter. 2. find the disk id of your hard disk3. create a diskpart txt script file to a. remove the cd-rom drive letter, b. assign cd a letter (r: in my case) c. create another partition on hard disk with remaining space and assign it letter e:4. Creates a batch file that a. launches diskpart with the above script file b. formats the newly created partition. We have to do it from a batch due to diskpart runs in its own little command line and want to know when it is complete so we can launch format as soon as it has created the partition.I had to do this due to the systems we use at work need a e: partition and the cd at r:, we have a zip so windows always put the cd at defualt of e: and you can never know if windows will call the hard disk disk 1 or disk 0.I call this script from GuiRunOnce in Unattended with no support files.************************DoDrives.vbs*********************Const FOR_READING = 1, FOR_WRITING = 2ComputerName = "."strComputer = "."Dim TFDIM NewFileDIM DefaultStringDIM CDDeviceIDDIM DiskNumDefaultString = Array ("select volume ", "remove noerr", "assign letter r", "select disk ", _ "Create partition primary", "assign letter e", "exit")NewFile = "C:\install\Disk.txt" 'We have to create a .txt script file for diskpartNewBat = "C:\install\edrive.bat" 'We have to use a bat so we can launch format after we are done with diskpartSet objFSO = CreateObject("Scripting.FileSystemObject")Set objWMIService = GetObject("winmgmts:" _& "{impersonationLevel=impersonate}!\\" _& strComputer & "\root\cimv2")Set colDisks = objWMIService.ExecQuery _("Select * from Win32_LogicalDisk")For Each objDisk in colDisksSelect Case objDisk.DriveTypeCase 1Case 2Case 3Case 4Case 5CDDeviceID = objDisk.DeviceIDCase 6Case ElseEnd SelectNextSet wmiServices = GetObject ( _"winmgmts:{impersonationLevel=Impersonate}!//" _& ComputerName)Set wmiDiskDrives = wmiServices.ExecQuery ( _"SELECT Caption, DeviceID FROM Win32_DiskDrive")For Each wmiDiskDrive In wmiDiskDrivesquery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _& wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition" Set wmiDiskPartitions = wmiServices.ExecQuery(query)For Each wmiDiskPartition In wmiDiskPartitionsSet wmiLogicalDisks = wmiServices.ExecQuery _("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _& wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") For Each wmiLogicalDisk In wmiLogicalDisksIf wmiDiskDrive.DeviceID = "\\.\PHYSICALDRIVE0" thenDiskNum="0"End IfIf wmiDiskDrive.DeviceID = "\\.\PHYSICALDRIVE1" thenDiskNum="1"End If Set TF=objfso.CreateTextFile(NewFile, True) 'Create a new script text fileTF.WriteLine (DefaultString(0)) & CDDeviceID 'select volume x: (x = CD Drive Letter)TF.WriteLine (DefaultString(1)) 'remove noerrTF.WriteLine (DefaultString(2)) 'assign letter rTF.WriteLine (DefaultString(3)) & DiskNum 'select disk x (0,1)TF.WriteLine (DefaultString(4)) 'Create partition primaryTF.WriteLine (DefaultString(5)) 'assign letter eTF.WriteLine (DefaultString(6)) 'exitTF.close 'Close the new fileSet TF=objfso.CreateTextFile(NewBat, True) 'Create a new batch fileTF.WriteLine "%windir%\system32\diskpart.exe /S c:\install\disk.txt > c:\install\drivelog.txt" 'This line launches diskpartTF.WriteLine "echo Y | %windir%\system32\format e: /fs:ntfs /q /v:""" 'This line formatsTF.close 'Close the new fileSet objShell = WScript.CreateObject("WScript.Shell")objShell.Run("%comspec% /K c:\install\edrive.bat"), 1, true 'Run the bat fileNextNextNext'''''End od CodeThanks gunsmokingman for putting the three scripts into one which led me making just one file to do it all. Edited December 3, 2005 by Tracy7011
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