Jump to content

Rename Drive Letters


mbkowns
 Share

Recommended Posts

I would like to make an AutoIT script that renames drive letters if they are in use. We got many new systems that came with USB card readers that took some of the network drive letters we use. Another problem we have is USB sticks / hard drives that end up using out network letters. I would say its maybe 1% of users getting this problem which isn't very many but I would like to make sure this doesn't happen anyway.

Persistant Mappings aren't a problem becuase you can delete them.

net use q: /d

So after I detect a drive letter in use with

DriveGetDrive

I need to be able to rename the drive letter to a defined list of drive letters.

Renaming CDROMS, Local Hard drives, USB sticks, USB card readers.

I have looked at diskpart but I am not sure how I could use this in my situtation. I could parse the diskpart log and make changes from that but I think that is kinda sloppy anyother ideas?

Thanks

Link to comment
Share on other sites

You can set the drive letter from registry key.

You can find the registry here: HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices

Use Reg functions to modify the letter of drive.

Maybe exists other best way.

I forgot to mention that I know about that and its easy also but it won't change the letters until next reboot.

Link to comment
Share on other sites

You can convert this vbs to AutoIt

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colVolumes = objWMIService.ExecQuery _
    ("Select * from Win32_Volume Where Name = 'D:\\'")

For Each objVolume in colVolumes
    objVolume.DriveLetter = "Q:"
    objVolume.Put_
Next
Link to comment
Share on other sites

Nope, that's the command... Convert to AutoIt anyway.

$objWMIService = ObjGet("winmgmts:\\.\root\cimv2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_Volume WHERE Name= 'Q:\\'")
For $colItem In $colItems
    $colItem.DriveLetter = "S:"
    $colItem.Put_
Next

Something missing here?

Link to comment
Share on other sites

My bad, I did not look closely enuf' to not confuse the _ in $colItem.Put_ with a line continuation. :)

Nope, that's the command... Convert to AutoIt anyway.

$objWMIService = ObjGet("winmgmts:\\.\root\cimv2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_Volume WHERE Name= 'Q:\\'")
For $colItem In $colItems
    $colItem.DriveLetter = "S:"
    $colItem.Put_
Next
Link to comment
Share on other sites

BTW:

The WMI class "Win32_Volume" is NOT available on Windows 2000 and Windows XP. Does anybody know a way to change a drive letter there without having to reboot?

The "Disk Management" mmc can do it, so why can't we?

The code above your post worked for me renaming a driver letter so maybe you didn't fully test it change Q to the drive want to change and S to the drive you want it to change to.

I need to play with it more to see if it will work for my purpose though.

Link to comment
Share on other sites

I have but it doesn't work with all the drive types I want to rename.

Are you very familiar with diskpart?

I have no trouble using it to re-assign drive letters for

CDROMS, Local Hard drives, USB sticks

The only one I haven't tried is a USB card reader that but shouldn't be any different.

Using it with a diskpart script file works great.

More diskpart info at http://support.microsoft.com/kb/300415

Link to comment
Share on other sites

Are you very familiar with diskpart?

I have no trouble using it to re-assign drive letters for

The only one I haven't tried is a USB card reader that but shouldn't be any different.

Using it with a diskpart script file works great.

More diskpart info at http://support.microsoft.com/kb/300415

Does it rename and let you use the renamed letter without rebooting? show me your script

Link to comment
Share on other sites

Does it rename and let you use the renamed letter without rebooting? show me your script

Yes! Did you even try anything???

For easiest automation, first you create a diskpart script (not an AutoIt script) that contains the diskpart commands to execute.

Then, use AutoIt to run diskpart with its script file.

Here is the diskpart script file to change a drive wiith the letter "E" to letter "G"

SELECT VOLUME E
ASSIGN LETTER G

Save that with a name like "E2G.txt". Note: the extension doesn't matter.

Then use AutoIt to run diskpart and execute the saved script (note: for English OS only, tested on XP)

#include <Constants.au3>
$pid = Run(@SystemDir & '\diskpart.exe /s "' & @ScriptDir & '\E2G.txt"', '', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

While 1
    $dpResult = StdoutRead($pid)
    If @error Then ExitLoop
    Select
        Case StringInStr(StringUpper($dpResult), "NO VOLUME SPECIFIED") Or StringInStr(StringUpper($dpResult), "THE ARGUMENTS YOU SPECIFIED FOR")
            MsgBox(4096 + 16, "Error", $dpResult)
            ExitLoop
        Case StringInStr(StringUpper($dpResult), "DISKPART SUCCESSFULLY ASSIGNED")
            MsgBox(4096, "Success", $dpResult)
    EndSelect
WEnd

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

Of course, you can get creative and have your AutoIt script dynamically create the diskpart script file on the fly based on your use of DriveGetDrive() and DriveGetType() etc. And you can create your own custom success/error messages.

If you look at Greenhorn's post http://www.autoitscript.com/forum/index.ph...st&p=468408 in that thread I pointed to earlier, you will see a method of parsing the output to check the results of diskpart commands issued to list the drives, etc.

Edit: Fixed a incorrect variable in the StderrRead(); damn cut-n-paste...

Edited by ResNullius
Link to comment
Share on other sites

  • 1 month later...

The code above your post worked for me renaming a driver letter so maybe you didn't fully test it change Q to the drive want to change and S to the drive you want it to change to.

Then probably you use Vista. As I said: the class is not existent on pre-Vista OSses, so it definitely won't work. Just google "XP Win32_Volume" and you will see my statement confirmed within the first three results. Maybe you tried one of the other solutions before and that already had worked, but you didn't notice. Using WMI and Win32_Volume DOES NOT WORK on XP.

Just use my solution:

http://www.autoitscript.com/forum/index.ph...04&hl=drive

Edited by cherdeg
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...