Geeks With Blogs
Tex-blog Mobile and other stuff
Suppose that you have the need to switch drive letters of two of yours volumes. Lets say you have one partition with assigned drive letter D and other drive with assigned drive letter E. To change those two drive letters, you normally would go thru control panel to computer management, where you would be able to change your partition assignments, that’s a lot of steps to be done. To make it shorter I wrote following short batch file. It uses diskpart command line tool that comes with Windows XP. (I must warn you that this tool provides some very dangerous commands that can actually destroy your disk data so use following script and diskpart tool at your own risk). 

So before using this script you need to learn volume numbers assigned to partitions with drive letters you want to switch. First open command prompt (WinKey + R, write “cmd”, hit Enter), then enter command “diskpart”, and then “list volume”. Console should look like this:

C:\Documents and Settings\Administrator>diskpart
Microsoft DiskPart w wersji 5.1.3565
Copyright (C) 1999-2003 Microsoft Corporation.
Na komputerze: AQ7400

DISKPART> list volume

  Wolumin ### Lit  Etykieta     Fs     Typ         Rozmiar  Stan       Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
  Wolumin 0     E                       DVD-ROM         0 B
  Wolumin 1     F                       DVD-ROM         0 B
  Wolumin 2     C                       NTFS   Partycja      48 GB  Zdrowy     System
  Wolumin 3     D   Disk1         NTFS   Partycja      20 GB  Zdrowy
  Wolumin 4     E   Disk2          NTFS   Partycja    7567 MB  Zdrowy

DISKPART>

Suppose I want to switch disk D and E, so this means I am switching Volume 3 and Volume 4. Below is the script code that you should copy/paste (with any minor changes) to SwitchDriveLetters.bat file:

rem Depending on your language, change “Volume” to whatever diskpart’s “list volume” shows in first column
@for /f "tokens=3" %%x in ('echo list volume ^| diskpart ^| findstr /c:"Volume %1"') do set v1=%%x
@for /f "tokens=3" %%x in ('echo list volume ^| diskpart ^| findstr /c:"Volume %2"') do set v2=%%x

echo select volume %1 > SwitchDisks.csp
echo remove letter %v1% noerr >> SwitchDisks.csp
echo select volume %2 >> SwitchDisks.csp
echo remove letter %v2% noerr >> SwitchDisks.csp
echo select volume %1 >> SwitchDisks.csp
echo assign letter %v2% >> SwitchDisks.csp
echo select volume %2 >> SwitchDisks.csp
echo assign letter %v1% >> SwitchDisks.csp

diskpart /s SwitchDisks.csp
del SwitchDisks.csp

To use it just enter in command prompt:

SwitchDriveLetters.bat 3 4

Where 3 and 4 are volumes which drive letters are supposed to be switched. Above script works as follows, it first reads drive letters for two volumes provided as the batch file arguments. Then it writes external script file SwitchDisk.csp. This script file will be provided as an argument to diskpart tool, and will first remove drive letters for both volumes, and then assigne exchanged drive letters. After that SwitchDisk.csp is deleted as a last step.

Hope this will be helpfull to someone. I’am still experiencing some problems with this method. Sometimes it is very slow on my laptop, and ie. taking out CD disk helps in such situation. Also remember to close all programs, using disks which drive letters you want to exchange. If there will be any error during assignment of new letters, then one or both of the volumes might have no letters assignments, don’t worry – you can fix it in Computer Management panel by reassigning letters.

Any way use this script at your own risk, and always backup important files.

Posted on Sunday, September 9, 2007 5:25 PM | Back to top


Comments on this post: Exchange Volume Letters batch script (WinXP)

# re: Exchange Volume Letters batch script (WinXP)
Requesting Gravatar...
TKZ... I've tried your script and works just GREAT... tkz

i put this script working on nlite.cm_ and works ;)
Left by Jorge Rebelo on Feb 26, 2009 7:08 PM

# re: Exchange Volume Letters batch script (WinXP)
Requesting Gravatar...
Great, but remember to leave a diskpart utility open anywhere because the volume number can change at each run of the utility...
support.microsoft.com/kb/937252
Left by Mr Thierry on Mar 12, 2013 2:24 AM

Your comment:
 (will show your gravatar)


Copyright © Martinez | Powered by: GeeksWithBlogs.net