Batch file to remove and reassign drive letters

batchbootdiskpart

I have what I believe to be a unique issue.
I don't do a lot of work with batch files, but I think that would be my best option.
Here is the situation. I have special software that looks for a perticular cd tray on my system then assigns that letter in the software. The drive I have isn't working, but it does authorize the software to assign that drive as a cd tray. Once the sofware is up and running I can manage my drive letters, swap the working one for the one it sees and then all works out fine.
I need to start by making sure I have a set drive letter on both of my drives which I can do at launch using a startup batch file:

Drive 1 : Y
Drive 2 : X

Then after a set amount of time for the software to boot and load the drive, swap the letters.

Drive 1 : X
Drive 2 : Y

To do this I was looking into using a diskpart script, but can't seem to get it to work through a batch file.

volume 0 remove drive letter
volume 1 remove drive letter // this way they are both blank so I don't have conflicting drive letters when I change one without the other
volume 0 set drive letter="Y"
volume 1 set drive letter="X"
wait 5 minutes // software will see volume 1 as X drive
volume 0 remove drive letter
volume 1 remove drive letter
volume 0 set drive letter="X"
volume 1 set drive letter="Y"
// now the software will see X drive as volume 0 making it all work OK.

Might sound weird, but it works when I do it all manually. I need to automate the process for people who start the machine, but don't know how or have time to set it up manually.

thanks for the help.

Best Answer

You can't script with diskpart using batch/cmd commands. Or PowerShell, for that matter. And it doesn't accept arguments like you'd expect it to when you call diskpart.exe. Very irritating, but if you want to programmatically control diskpart, you have to use "diskpart scripts", which are basically just line-break delimited diskpart commands in a text file.

You should be able to call your diskpart script from a batch file with the syntax: diskpart /s scriptname.txt.

Related Question