1

Using a .bat script on an USB drive I'd like to change it's drive letter without using 3rd party software and any requirements on the system where the USB is plugged in except that it's Windows XP or higher.

To get the current drive letter I use

set DRIVE=%~dp0

Which is E: for example

Before I can actually change E:'s drive letter, how do I find out the volume number in diskpart's volume list automaticly?

select volume E:

obviously won't work, you can only use the n for the disc number.

EDIT:

Thanks to @wOxxOm for the solution. Here is my final .bat script i now use to auto-change the drive letter of the drive the script is on to U:\

@echo off
set DRIVERAW=%~dp0
set DRIVE=%DRIVER:~0,1%
if %DRIVE%==U exit
for /f "tokens=2,3" %%a in ('echo list volume ^| diskpart') do (
    if %%b==%DRIVE% set VOLNO=%%a
)
del %DRIVERAW%\diskpart.txt
echo select volume %VOLNO% > %DRIVERAW%\diskpart.txt
echo assign letter=U >> %DRIVERAW%\diskpart.txt
echo ^G
diskpart /s %DRIVERAW%\diskpart.txt
exit

You could replace the two U's with any other drive letter you want if it should not be mounted to U:\

Just stay sure that nothing is already mounted on U:\

RapidFireArts
  • 83
  • 1
  • 11
  • Apparently a [duplicate](http://serverfault.com/questions/62578/how-to-get-a-list-of-drive-letters-on-a-system-through-a-windows-shell-bat-cmd) if the question is only about finding the drive letter. – wOxxOm Aug 25 '15 at 20:15
  • @wOxxOm Please read before saying it's a duplicate, i already read everything about finding drive letters but that's not my question here. I know the drive letter but i need it's volume number used in diskpart. – RapidFireArts Aug 25 '15 at 20:19
  • @wOxxOm i'm sorry but i don't get how thats working. i tried running `echo list volume | diskpart` in a bat file and in cmd manually but all it does is opening diskpart. i'm not familiar with parsing outputs using for /f even though i've used cmd a lot before :/ – RapidFireArts Aug 25 '15 at 20:33

3 Answers3

1

Parse the list of volumes which looks like this:

Volume 6 E MY_USB FAT32 Removable 971 MB Healthy

Run in elevated command prompt or rightclick the .bat file and run as admin.

for /f "tokens=2,3" %%a in ('echo list volume ^| diskpart') do (
    if %%b==E echo Volume number is %%a
)

You can also check by volume name (use tokens=2,4) or by volume type (use tokens=2,6), a little trickery with token numbers will be required in case volume name contains spaces.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • thanks for your quick response, but it still only opens up the diskpart window and nothing happens (closing returns nothing and the script ends). it shouldn't be different for windows 10 than any other version but could that be the problem? it's a fresh installation. sorry for being so problematic... script is exactly `@echo off NL for /f "tokens=2,3" %%a in ('echo list volume ^| diskpart') do ( NL if %%b==U echo Volume number is %%a NL ) NL pause` ( NL is next line) – RapidFireArts Aug 25 '15 at 20:38
  • Run `echo list volume | diskpart` in a command prompt and compare the output to the reference in the answer. – wOxxOm Aug 25 '15 at 20:41
  • I don't have the exact syntax at hand but it might work better if you try to receive that information by using wmic – Marged Aug 25 '15 at 20:47
  • @Marged, yes but I heard WMIC may be disabled/nonfunctional, especially on the older XP. Anyway I don't see the ordinal volume number suitable for diskpart in the output of `wmic logicaldisk get` – wOxxOm Aug 25 '15 at 20:51
  • @wOxxOm oh okay i tried running an elevated cmd and now it works, sorry, i know that i needed admin rights to change the drive letter but didn't try it for getting the volume number. thanks a lot, i'll post my solution here when im done creating the script. – RapidFireArts Aug 26 '15 at 09:17
  • @wOxxOm One more question: can this method get confused by other letters in the volume labels or does it only search inside the Ltr columns? – RapidFireArts Aug 26 '15 at 09:20
  • 1
    The lines start with `Volume` `Number` `Letter` `Label` so the letter token is always third, and the volume label is (or starts with) the fourth token but may span to more tokens (5th, etc) in case it has spaces. – wOxxOm Aug 26 '15 at 09:33
0

The Third line in the batch file, set DRIVE=%DRIVER:~0,1% should be, set DRIVE=%DRIVERAW:~0,1%

With that change it works for me.

Amit Verma
  • 8,660
  • 8
  • 35
  • 40
0

Much more easiear as You think :) Not documented, but working!

SELECT VOLUME E

Just use the volume letter instead of number...

See my result:

C:\Temp>diskpart

Microsoft DiskPart version 10.0.19041.964

Copyright (C) Microsoft Corporation.
On computer: DESKTOP-H6E03H5

DISKPART> select volume e

Volume 3 is the selected volume.

DISKPART> list volume

  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
  Volume 0     C                NTFS   Partition   1862 GB  Healthy    Boot
  Volume 1         Helyreállít  NTFS   Partition    529 MB  Healthy    Hidden
  Volume 2                      FAT32  Partition     99 MB  Healthy    System
* Volume 3     E                NTFS   Removable    232 GB  Healthy
  Volume 4     D                RAW    Removable    232 GB  Healthy

DISKPART>
sz9
  • 95
  • 8