DOS Batch script to find Drive by label

McG
McG used Ask the Experts™
on
Hello all,
I'm fairly sure this has been asked a hundred times before but I can't seem to find the answer anywhere.  I turn to you the Dos experts for help.

Basically I need to run a Dos batch job in Windows 2000 and XP to find a removable drive labelled "ATA" and set it as a variable.  I've done something similar using the date | find piped command but only for taking user input and setting it as a variable.

Your help is much appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:

McG,

Here's a batch script to get a disk's Volume Label
and write it to an environment variable named %V% ...

::
@echo off
cls
set v=
vol c:>~rin1010.bat
echo set v=%%5 %%6>volume.bat
call ~rin1010.bat
for %%x in (volume.bat ~rin1010.bat) do del %%x
echo.
echo    Volume Label in Drive C: =  %v%
echo.
::


The above example is coded to get the Volume Label from the C: hard drive...
It can be altered to also work on removable drives (e.g., floppy drives, etc.)
but contains no error checking, such as when there's no disk in the drive,
although it can be edited to manage variable conditions such as those.
It also handles Volume Labels having a 'space' in its name and can
easily be modified to handle multiple spaces if that's necessary.

As written, it merely echoes the Volume Label to the screen,
or else indicates whether the disk has "no label" ...
Reference the %V% environment variable
to use for your specific purpose...

You mention wanting to "find a removable drive labelled "ATA" and set it as a variable."
Do you mean that you need to test whether a drive having that label exists?
If you know the Volume Label then can you just set the variable?
Or do you mean get and set the drive letter to a variable?

Below is a batch file method to get the drive letter
of a drive having a specified Volume Label...
This is for DOS mode and Windows 9.x
and needs modifying to work under XP and 2K ...
The CTTY command isn't supported by CMD.EXE
and the interpreter is referenced as %comspec% etc...
(The CTTY commands are merely to clean up the display,
aren't necessary for the script to function and can be removed.)
But I'll post the batch file here and maybe it'll give you some ideas...
Change the "ATA" reference to whatever Volume Label you're seeking...

::
@echo off
cls
set v=
if %1~==~ %0 c d e f g h i j k m n o p q r s t u v w x y z
ctty nul
:~rbit
if %1~==~ goto ~done
%comspec%/f/c vol %1:|find/i "ATA"|if errorlevel 1 for %%x in (shift goto:~rbit) do %%x
set v=%1
:~done
ctty con
echo.
if %v%~==~ echo    Drive Not Found...
if not %v%~==~ echo    Drive Letter =  %v%
::


Please post back with further details if you want other methods
or if these batch file examples aren't what you're needing
or if you have questions about using any of it...
 
McG

Author

Commented:
Sorry I meant the latter - "Or do you mean get and set the drive letter to a variable?"

Thanks for the batch files the one at the end is what I'm looking for as I needed to find a drive labelled "ATA".

Thanks again for your help.
McG

Author

Commented:
I might have been a bit quick off the mark.  One problem is that cmd.exe doesn't support "for".  You can run the batch file without ctty as expected but "For was unexpected at this time" message will appear and output the drive as C:.  The first drive it sees.

Any more ideas would be appreciated.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial