Link to home
Create AccountLog in
Avatar of Julian Parker
Julian ParkerFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Find drive letter of a named drive

Hi,

I want to write a simple backup batch file to backup to a USB drive amongst other things.

I can list the volume using diskpart /s diskscript.txt where diskscript.txt has "list volume" in it and then search for the diskname;
eg/
   diskpart /s diskscript.txt | find "BACKUP"

I now need to extract the disk volume from the string and assign it to a variable.
  Volume 4     F   BACKUP    FAT32  Removeable  1934 MB

I don't want to hard code the drive letter in the script as it may change. If awk was available as standard I'd be ok :-).

Is there a MSDOS alternative?

O/S is Windows XP Home/Professional.

I will up the points for a quick response... hell I may even do it anyway! :-)


ASKER CERTIFIED SOLUTION
Avatar of PWeerakoon
PWeerakoon
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Also, if you want to assign the drive letter to a variable directly, replace "do echo %k" with "do set driveletter=%k"

then "echo %driveletter% to see what happens.

Avatar of Julian Parker

ASKER

thx,

so far I have this;
   echo list volume > disklist.inf
   diskpart /s disklist.inf | find /i "CATHERINE" > vollist.inf
   for /f " tokens=3 " %%I in (vollist.inf) do set USBDRIVE=%%I:

You mentioned it's possible to get rid of the output files I create, I tried this;

   for /f "tokens=3" %%i in (echo list volume | diskpart /s | find /i "BACKUP") do echo %%i:

which seemed logical to me...until it failed :-)

SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
with pipe, and only drive name, for use in a batch file:

for /F "tokens=3" %%D in ('echo list vol ^| diskpart ^| find "BACKUP"') do set drive=%%D

Note the ^|, the carret is required to "escape" the pipe so the shell does not interpret it on first pass (but on second, when for argument is executed).
I get an error;

%%D was unexpected at this time.

Use the "usebackq" option.

for /f "usebackq tokens=3" %%i in (`echo list volume | diskpart /s | find /i "BACKUP"`) do echo %%i


Here's a snippet from the help file...

  Finally, you can use the FOR /F command to parse the output of a
  command.  You do this by making the file-set between the
  parenthesis a back quoted string.  It will be treated as a command
  line, which is passed to a child CMD.EXE and the output is captured
  into memory and parsed as if it was a file.  So the following
  example:
 
    FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
 
  would enumerate the environment variable names in the current
  environment.

Open in new window

No, usebackq is not of any use at the moment. It changes the way quotes are interpreted. The pipes have to be escaped anyway.

My line with %%D was for use in batch file, as already posted. If you want to try it on commandline, use only %D:

for /F "tokens=3" %D in ('echo list vol ^| diskpart ^| find "CATHERINE"') do set usbdrive=%D
sorry,

I knew that... just getting confused in my old age :-)

all sorted thanks, upping the points...
Thanks chaps, great work!

It was so the missus could do a backup without me and I needed to cater for almost all eventualities!