Jump to content

USB drive letter


Recommended Posts

I am new to this site and have played a little with autoit.

Has anyone come up with an autoit script to detect a usb drive AND force it to a specific drive letter. We have multiple computers in our organizaiton and users have created their own batch backup files. I have seen the scripts to find a specific file on a usb drive, which do work. But I am at a loss as how to force the drive letter to change.

Link to comment
Share on other sites

The OS is XP-Pro with a few XP-home.

I've been playing with the "usbletter.exe" utility from Novell. It will at least give the user a chance to change the drive letter manually (and easily) If I add the command "usbletter x" it will default to x: as the first choice for a letter and the user just has to click OK.

Unfortunately, if I run that utility a bunch of times (durring testing) it locks up and I have to go to Task Mgr to shut them down.

Link to comment
Share on other sites

O.K., here we go ...

I didn't tested it, cause I don't want to curl my Drive Letters. :D;)

But the Syntax is O.K. ...

If you have a Testmaschine, give it a trial, but do it on your own risk !!! :P

Description:

Create a Textfile on the root Directory of your Pen Drive, name it 'USB_Pen.txt'.

Put the following content into this file and save it:

list volume
  exit

Here comes the Script.

It will create a second textfile 'diskpart.txt' on your Pen Drive ...

You can customize the way yu want the Script to run, if you want.

I left the Debug lines in there for testing ... :P

Tomorrow I will append more Comment lines at the Script (too lazy and tired now ...)

#include <Array.au3>

$sDrvLetterNew = InputBox('Select new Drive Letter', 'Select Drive Letter to assign, please !')
If @error = 1 Then Exit ; Cancel Button pushed.
$arDrvList = DriveGetDrive('REMOVABLE') ; Get list of removable Drives.

For $i = 1 To $arDrvList[0]
    If $arDrvList[$i] = 'a:' Then ContinueLoop ; except floppy Drive.
    If FileExists($arDrvList[$i] & '\USB_Pen.txt') Then
        $sDrvLetter = $arDrvList[$i]
        $sDrvLabel = DriveGetLabel($arDrvList[$i])
    EndIf
Next

$PID = Run(@SystemDir & '\diskpart.exe /s "' & $sDrvLetter & '\USB_Pen.txt"', '', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
$arLine = _StreamRead($PID) ; Read out Volume number of Pen Drive.
;_ArrayDisplay($arLine)
For $i = 1 To $arLine[0]
    ;MsgBox(0, "", $arLine[$i])
    If StringInStr($arLine[$i], $sDrvLabel) Then
        ;MsgBox(0, "", $arLine[$i])
        $arDrvVolume = StringRegExp($arLine[$i], '(Volume \d\d?)', 1)
    EndIf
Next
;_ArrayDisplay($sDrvVolume)
If $arDrvVolume Then
    Dim $fhTextCommands = FileOpen($sDrvLetter & '\diskpart.txt', 2)
    $sTextCommands = 'select ' & $arDrvVolume[0] & @CRLF & 'assign letter ' & $sDrvLetterNew & @CRLF & 'exit'
    FileClose(FileWrite($fhTextCommands, $sTextCommands))
EndIf

$iMsg = MsgBox(4145, "Let's get it on ...", 'Are you sure to continue ?')
If $iMsg = 2 Then
    Exit
Else
    Run(@SystemDir & '\diskpart.exe /s "' & $sDrvLetter & '\diskpart.txt"', '', @SW_HIDE)
EndIf

Exit

Func _StreamRead($PID)

    While 1
        $line = StdoutRead($PID)
        If @error Then ExitLoop
        ;MsgBox(0, "STDOUT read:", $line)
        $arLine = StringSplit($line, @LF)
    WEnd

    While 1
        $line = StderrRead($PID)
        If @error Then
            ExitLoop
        EndIf
        MsgBox(0, "STDERR read:", $line)
    WEnd
    Return $arLine

EndFunc   ;==>_StreamRead

I hope it works and it is the way you want, else I wastet my time ...

Roger, over and out ! (No, I didn't ment you, Roger ! ;) )

Greetz

Greenhorn

Edited by Greenhorn
Link to comment
Share on other sites

Heres my version:

$oForce = "W:"
$oRemove = True

If $oRemove = True Then
    RunWait(@ComSpec & " /c subst /d " & $oForce, "", @SW_HIDE)
Else
    For $i = 65 To 90
        If FileExists(Chr($i) & ":\usb") Then
            RunWait(@ComSpec & " /c subst " & $oForce & " " & Chr($i) & ":\", "", @SW_HIDE)
            Exit
        EndIf
    Next
EndIf

Just make a file called "usb" on the usb drive, And run the script.

$oForce =, In your case "X:"

$oRemove =, True to "remove" the "redirection", False to "install" it.

EDIT::

You "could" use DriveGetDrive, or DriveGetType.

But I prefer my way x3

And, I believe the "redirection" will delete itself on reboot too.

Edited by AzKay
# MY LOVE FOR YOU... IS LIKE A TRUCK- #
Link to comment
Share on other sites

Heres my version:

$oForce = "W:"
$oRemove = True

If $oRemove = True Then
    RunWait(@ComSpec & " /c subst /d " & $oForce, "", @SW_HIDE)
Else
    For $i = 65 To 90
        If FileExists(Chr($i) & ":\usb") Then
            RunWait(@ComSpec & " /c subst " & $oForce & " " & Chr($i) & ":\", "", @SW_HIDE)
            Exit
        EndIf
    Next
EndIf

Just make a file called "usb" on the usb drive, And run the script.

$oForce =, In your case "X:"

$oRemove =, True to "remove" the "redirection", False to "install" it.

EDIT::

You "could" use DriveGetDrive, or DriveGetType.

But I prefer my way x3

And, I believe the "redirection" will delete itself on reboot too.

You create just a virtual Drive with SUBST !
Link to comment
Share on other sites

Oh, sorry, my dear, I was too tired yesterday ... :D

I changed the name of the variable from $s... to $ar..., cause it's an array which is returned from $arDrvVolume = StringRegExp($arLine[$i], '(Volume \d\d?)', 1)

Just change '$sDrvVolume' to '$arDrvVolume' and it's righty right ! :P

I'll correct the Script in my previous post, too !

Please let me know if it works properly for you !!!

Greetz

Edited by Greenhorn
Link to comment
Share on other sites

.. Point being?

It gets the job done, Wheres the problem?

If it does, it's fine ! :D

But I read also about problems by using it this way, cause you create just a virtual path to the 'real' path ...

http://newyear2006.wordpress.com/2006/09/1...gen-aktiv-sind/

@mreinhart

please test both Scripts, if it's ok for you !

Greetz to both of you !

Edited by Greenhorn
Link to comment
Share on other sites

  • 1 year later...

I am very new to scripting and programming (less than new!!!) I am interested in doing the same thing. Would you be able to provide a bit more instruction on how to get your script working? What kind of file to creat on the USB drive? where to run the script from (target system or developer system)?

Thanks

Mehrdad

Heres my version:

$oForce = "W:"
$oRemove = True

If $oRemove = True Then
    RunWait(@ComSpec & " /c subst /d " & $oForce, "", @SW_HIDE)
Else
    For $i = 65 To 90
        If FileExists(Chr($i) & ":\usb") Then
            RunWait(@ComSpec & " /c subst " & $oForce & " " & Chr($i) & ":\", "", @SW_HIDE)
            Exit
        EndIf
    Next
EndIf

Just make a file called "usb" on the usb drive, And run the script.

$oForce =, In your case "X:"

$oRemove =, True to "remove" the "redirection", False to "install" it.

EDIT::

You "could" use DriveGetDrive, or DriveGetType.

But I prefer my way x3

And, I believe the "redirection" will delete itself on reboot too.

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...