brian873 Posted May 29, 2007 Share Posted May 29, 2007 Hi I have a problem with assigning a drive letter in my script. At present the drive letter is given through an entry in an INI file. The problem lies when I use it on another computer and the drive letter is already taken. I need to manually change the INI file. I would like to automatically assign a free drive letter if the chosen one is not available. I cant work out how to do this. I have been trying with drivegetdrive but cant work out how to pipe it so it uses an available drive. Any help would be appreciated. thanks Link to comment Share on other sites More sharing options...
SkinnyWhiteGuy Posted May 29, 2007 Share Posted May 29, 2007 I use this with USB stuff, works for me, hopefully will you too: Local $Skip, $MountDrive, $TempDrive, $Drives, $FreeDrive Local $FreeDrives[23] = ["D:", "E:", "F:", "G:", "H:", "I:", "J:", "K:", "L:", "M:", "N:", "O:", "P:", "Q:", "R:", "S:", "T:", "U:", "V:", "W:", "X:", "Y:", "Z:"] $Drives = DriveGetDrive("all") For $FreeDrive in $FreeDrives $Skip = False For $TempDrive in $Drives If $FreeDrive = $TempDrive Then $Skip = True ExitLoop EndIf Next If $Skip = False Then $MountDrive = $FreeDrive ExitLoop EndIf Next At the end of that script running, $MountDrive should be the drive letter with proceeding colon of the first available drive letter, starting with D (to avoid floppy confusion). Note: This script could be done better (changing my array to dynamic calls to Chr()), but, it works, and works fast. Hope this helps Link to comment Share on other sites More sharing options...
Rick Posted May 29, 2007 Share Posted May 29, 2007 (edited) or... $All = DriveGetDrive("all") $NewDrive=CHR(ASC(StringUpper(StringLeft($All[$All[0]],1)))+1) & ":" msgbox(0,"", $NewDrive ) which gets the decimal value of last drive (capitalised) , adds 1, then converts it back to a letter and adds ":", which is fine so long as drives dont go beyond Z:, which is unlikely. Edited May 30, 2007 by Rick Who needs puzzles when we have AutoIt!! Link to comment Share on other sites More sharing options...
brian873 Posted May 30, 2007 Author Share Posted May 30, 2007 or... $All = DriveGetDrive("all") $NewDrive=CHR(ASC(StringUpper(StringLeft($All[$All[0]],1)))+1) & ":" msgbox(0,"", $NewDrive ) which gets the decimal value of last drive (capitalised) , adds 1, then converts it back to a letter and adds ":", which is fine so long as drives dont go beyond Z:, which is unlikely. Hi Rick. Thanks for the reply. Unfortunately I am getting the output [: in the message box with this script any idea how I can fix this? Thanks again Link to comment Share on other sites More sharing options...
brian873 Posted May 30, 2007 Author Share Posted May 30, 2007 Thanks SkinnyWhiteGuy that works a treat for me! Link to comment Share on other sites More sharing options...
Rick Posted May 30, 2007 Share Posted May 30, 2007 (edited) ok, i was at last drive first, and not looking at what was free inbetween in my first example, so i've done this to.... $All = DriveGetDrive( "all" ) $First = StringUpper(StringLeft($All[1],1)) Local $Found = "", $Next = ASC($First)+1 if $Next = 66 then $Next = $Next + 1 ; if b: found make it c: While $Found = "" if DriveGetType(CHR($Next) & ":") = "" then $Found = CHR($Next) & ":" $Next = $Next + 1 Wend MsgBox(4096,"Free Letter", $Found ) Edited May 30, 2007 by Rick meoit 1 Who needs puzzles when we have AutoIt!! Link to comment Share on other sites More sharing options...
brian873 Posted May 30, 2007 Author Share Posted May 30, 2007 Thanks Rick! I tested with my script and it works great! Thanks for taking the time to look at this. Link to comment Share on other sites More sharing options...
Danny35d Posted May 30, 2007 Share Posted May 30, 2007 MsgBox(0, 'Free Letter', _GetFreeDriveLetter()) Func _GetFreeDriveLetter() For $x = 67 To 90 If DriveStatus(Chr($x) & ':\') = 'INVALID' Then Return(Chr($x) & ':') Next EndFunc meoit 1 AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line Link to comment Share on other sites More sharing options...
brian873 Posted May 31, 2007 Author Share Posted May 31, 2007 Thanks for showing me another way to get the job done Danny! Link to comment Share on other sites More sharing options...
kylek29 Posted August 13, 2007 Share Posted August 13, 2007 Even though this topic is a few months old, I figured I'd post in it instead of starting a new one. Been searching for an example of some sort for the past hour. Would you mind sharing the script you have for changing the drive letter? I'm scripting a flashdrive menu (to launch when inserted), but some of the things I'm calling need a specific drive letter (programs on the PC). Plus, I like my flashdrive to be the same drive letter everytime (desktop shortcuts). I know I can assign it in diskmgmt, but that will sometimes go away (and revert to an unused letter). What I'm trying to do is just so that when the flashdrive is inserted, and the program is launched, it reassigns the flashdrive to "P:\" (or whatever). This would appear to be what you were trying to do as well. 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