Exchange Volume Letters batch script (WinXP)

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.

This article is part of the GWB Archives. Original Author: Martinez

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.