Jump to content

Auto Assign drive letter


Recommended Posts

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

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

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 by Rick

Who needs puzzles when we have AutoIt!!

Link to comment
Share on other sites

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

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 by Rick

Who needs puzzles when we have AutoIt!!

Link to comment
Share on other sites

  • 2 months later...

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...