Help running a script using Diskpart

jdd89

Honorable
Feb 9, 2014
3
0
10,510
I am trying to use a .bat file that will reassign the USB drive letter to Z regardless of which computer it is in. I have a few programs that need to keep a constant drive letter to work, and I am also trying to make this portable and easy to copy.

I currently have the .bat asking the user what drive was assigned, then I want it to select that drive and assign it to Z. Right now I am having an issue with using commands in Diskpart. It doesn't want to ask the user for input and instead just turns off. I am not sure if it is how I input it or what.

It briefly runs the script, but shuts off before I can see anything it did.After awhile of troublshooting I only placed "List Volume" in the script and I can see it flash the volumes and then shut off. I tried placing pause afterwards, but didn't have luck with that.

The script is located on the volume, in this case X So I enter X and press enter, I see a flash of volumes and then it shuts off.

This is the .Bat

Set/P VolNum=Please enter Volume number:
diskpart /s %VolNum%:\script.txt


This is the script

list volume
Pause
Set/P VolNum=Please enter Volume number:
Select Partition %VolNum%
Assign Letter=Z

The reason I have two Set/P was since a second window opened up and I was having issues. I would really prefer only one prompt.

I am fairly new to writing .bat and using diskpart automation, so try to break it down if possible.

If what I am doing is not possible, let me know and if you can think of an alternative I would appreciate it.

Thank you everyone!

 
Solution
Here's a script that should work:

.bat or .cmd file:
set /P _DriveLetter=Please enter drive letter:
echo >script.txt select volume=%_DriveLetter%
echo >>script.txt assign letter=Z
echo >>script.txt exit
diskpart /s script.txt
del Z:\script.txt /Q

Problem you have in your original script is that you can't use command line commands or environment variables inside the diskpart program, and using the drive letters directly is a lot more easier aswell.

Edit. You need to run the bat or cmd as administrator or it won't work.

Samat

Distinguished
Here's a script that should work:

.bat or .cmd file:
set /P _DriveLetter=Please enter drive letter:
echo >script.txt select volume=%_DriveLetter%
echo >>script.txt assign letter=Z
echo >>script.txt exit
diskpart /s script.txt
del Z:\script.txt /Q

Problem you have in your original script is that you can't use command line commands or environment variables inside the diskpart program, and using the drive letters directly is a lot more easier aswell.

Edit. You need to run the bat or cmd as administrator or it won't work.
 
Solution