Question : 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! :-)


Answer : Find drive letter of a named drive

Use the FOR command.
Lets say you have saved the output  "Volume 4     F   BACKUP    FAT32  Removeable  1934 MB" as OUTPUT.TXT

then use the FOR command as follows...
for /f "tokens=1,2,3 delims= " %i in (OUTPUT.TXT) do echo %k

The output of this command would give you "F" without the quotes.

The /f switch is to process a file line by line, in this case the single line in OUTPUT.TXT.
It extracts the first three tokens delimited by spaces (note the space between the = and the " after delims)
It assigns each token to variables stating with "i". So the first token is "Volume", second is "4" and the third is "F" which is the drive letter you need.  This is assigned to the variable "k".

Note if you are using this in batch mode you have to replace each percent (%) sign with two like %%i and %%k.


Also, if you don't want to save the output of your first script to a file "output.txt", you should be able to pipe that into the FOR command.

type FOR /? for more info.

Hope this helps.
Random Solutions  
 
programming4us programming4us