Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Checking whether parameter is a drive letter

1 view
Skip to first unread message

Rich Pasco

unread,
Jul 9, 2008, 1:07:30 PM7/9/08
to
Suggestions are invited on the simplest and most reliable way
(in a BAT file command script) to check whether a command-line
parameter (e.g. %1 or %2) designates a valid drive letter (with
its colon, e.g. C: or D: )?

- Rich

Rich Pasco

unread,
Jul 9, 2008, 1:19:58 PM7/9/08
to
Rich Pasco wrote:

This should work with Windows 2000 and newer.

Below is what I have now. I can't seem to fool it into making an
mistake, but the checking feels awkward. Does anyone have a more
elegant solution?

============================
set drive=%~d1
if /I not %~1==%drive% (
echo Parameter "%~1" does not name a drive.
goto end
)
if not exist "%drive%" (
echo Drive %drive% is not available.
goto end
)
echo drive is %drive%
... useful part of script goes here...
:end
============================

- Rich

Dean Wells (MVP)

unread,
Jul 9, 2008, 6:16:49 PM7/9/08
to
"Rich Pasco" <rich...@hotmail.com> wrote in message
news:%23nULHZe...@TK2MSFTNGP03.phx.gbl...

Rich,

Take a look at the post entitled 'Batch file to show Network drives', I
believe you'll find relevant info. in there.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l

Dean Wells (MVP)

unread,
Jul 9, 2008, 6:21:08 PM7/9/08
to
"Dean Wells (MVP)" <dwe...@maskmsetechnology.com> wrote in message
news:egGL$Fh4IH...@TK2MSFTNGP05.phx.gbl...

Perhaps something like this depending on support for the binary of
course -

fsutil fsinfo drives | findstr /i "F:" && echo Valid. || echo Invalid.

Rich Pasco

unread,
Jul 9, 2008, 7:25:49 PM7/9/08
to
Dean Wells (MVP) wrote:

>> Take a look at the post entitled 'Batch file to show Network drives',
>> I believe you'll find relevant info. in there.
>

> Perhaps something like this depending on support for the binary of
> course -
>
> fsutil fsinfo drives | findstr /i "F:" && echo Valid. || echo Invalid.

Thanks, Dean. Many of the solutions in that thread, including yours,
require fsutil, which doesn't seem to be present on my Windows 2000
system. Can I download it as an add-on, or is it limited to XP and
newer?

Learn about fsutil here
http://technet2.microsoft.com/windowsserver/en/library/3dd141a8-57b8-4bd8-81dd-69c515fd059f1033.mspx

Considering that I need to support Windows 2000, I may just stick with
my original solution, which does work under Windows 2000.

- Rich

Dean Wells (MVP)

unread,
Jul 9, 2008, 7:46:07 PM7/9/08
to
"Rich Pasco" <rich...@hotmail.com> wrote in message
news:OTp1gsh4...@TK2MSFTNGP02.phx.gbl...

Nod, fair comment, I confess my efforts regarding legacy-supportability
typically end at XP and 2K3.

Here's another hip-shot -

@echo off
set arg=%~1
set driveLETTER=%arg:~0,1%
if exist %driveLETTER::=%:\nul (
echo %driveLETTER::=%: is valid)
else (
echo %driveLETTER::=%: is invalid.
)

Al Dunbar

unread,
Jul 9, 2008, 9:06:23 PM7/9/08
to

"Dean Wells (MVP)" <dwe...@maskmsetechnology.com> wrote in message
news:evSA33h4...@TK2MSFTNGP02.phx.gbl...

See if this works at all for you:

@echo off

call:test C:
call:test c:
call:test c:d:fvfv\\wwwww
call:test "C:"
call:test "c:\"
call:test R:\dummy\asd.zxc
pause
goto:eof

:test
if /i "%~1" EQU "%~d1" (
echo/parameter IS a drive letter: "%~1"
) else (
echo/parameter is NOT a drive letter: "%~1"
)
goto:EOF

/Al


Rich Pasco

unread,
Jul 10, 2008, 3:45:02 AM7/10/08
to
Al Dunbar wrote:

> See if this works at all for you:
>
> @echo off
>
> call:test C:
> call:test c:
> call:test c:d:fvfv\\wwwww
> call:test "C:"
> call:test "c:\"
> call:test R:\dummy\asd.zxc
> pause
> goto:eof
>
> :test
> if /i "%~1" EQU "%~d1" (
> echo/parameter IS a drive letter: "%~1"
> ) else (
> echo/parameter is NOT a drive letter: "%~1"
> )
> goto:EOF
>
> /Al

Hi Al, I like it. It is the same method as I used in my own example,
my second post in this thread.

- Rich

Esra Sdrawkcab

unread,
Jul 10, 2008, 5:05:28 AM7/10/08
to

If running on an intel box, with debug in the path (or it could be
converted to ascii asm), this would do it:

@echo off
rem qdrvl.bat

echo E100 A0 5C 00 B4 4C CD 21> %temp%\qdrvl.dis
echo Rcx>> %temp%\qdrvl.dis
echo 7 >> %temp%\qdrvl.dis
echo w >> %temp%\qdrvl.dis
echo q >> %temp%\qdrvl.dis
echo create>%temp%\qdrvl.com
debug %temp%\qdrvl.com<%temp%\qdrvl.dis >nul
%temp%\qdrvl.com %1
if errorlevel 243 goto isnumbercolon
if errorlevel 1 echo valid drive
if not errorlevel 1 echo Not a valid drive
goto exit
:isnumbercolon
echo number followed by colon detected!
:exit
::del %temp%\qdrvl.com
del %temp%\qdrvl.dis


though it seems the com parser is a bit crude and allows $: and £:

(X-posted to amb)

Al Dunbar

unread,
Jul 10, 2008, 10:45:41 PM7/10/08
to

"Rich Pasco" <rich...@hotmail.com> wrote in message
news:%239LXeDm...@TK2MSFTNGP05.phx.gbl...

Sorry, I didn't recognize its similarity because you use a different IF
statement syntax.

/Al


Rich Pasco

unread,
Jul 11, 2008, 5:54:52 PM7/11/08
to
Esra Sdrawkcab wrote:

Esra, while I appreciate your intention to help, I was asking for a
more elegant solution than my original one, which was just a few lines
in a BAT file. A debug script to generate a 16-bit DOS COM file (which
is only supported under 32-bit Windows for backwards compatibility) is
not, in my opinion, more "elegant" than I had.

Regarding the cross-post, I would prefer that discussion be constrained
to the forum in which I posted the question. I am not interested in
supporting MS-DOS anyway.

- Rich

foxidrive

unread,
Jul 11, 2008, 6:35:14 PM7/11/08
to
On Thu, 10 Jul 2008 00:45:02 -0700, Rich Pasco <rich...@hotmail.com>
wrote:

Except Al's syntax won't choke on spaces etc.

Richard Bonner

unread,
Jul 14, 2008, 8:44:03 AM7/14/08
to
(For some reason no original post appeared on my serevr. As such, this
suggestion may not be valid for the purpose.)

One might use XSET 's "DRIVETEST" feature to determine if a parameter
is a valid drive.

The syntax is:

XSET TEST DRIVETEST A:

This will assign a variable to "TEST" such as:

READABLE
WRITEABLE
REMOVABLE
INVALID

...and a number of others. One could then test that variable for a
variety of outcomes.


One or more XSET links can be found at:

http://www.chebucto.ca/~ak621/DOS/Websites.html


Richard Bonner
http://www.chebucto.ca/~ak621/DOS/

Rich Pasco

unread,
Jul 14, 2008, 1:41:25 PM7/14/08
to
foxidrive wrote:

> Except Al's syntax won't choke on spaces etc.

Good catch, foxidrive!

Here is my revised test:

set drive=%~d1
if /I not "%~1"=="%drive%" (

Rich Pasco

unread,
Jul 14, 2008, 1:46:30 PM7/14/08
to Richard Bonner

Richard Bonner wrote:

> (For some reason no original post appeared on my serevr. As such, this
> suggestion may not be valid for the purpose.)

The original post was in microsoft.public.win2000.cmdprompt.admin
The cross-post to alt.msdos.batch was inappropriate.

> One might use XSET 's "DRIVETEST" feature to determine if a parameter

> is a valid drive....

Thank you, but the question was about doing it in a BAT script. I was
not looking for anything requiring an external executable helper app.

- Rich

Richard Bonner

unread,
Jul 14, 2008, 2:49:51 PM7/14/08
to
Rich Pasco (rich...@hotmail.com) wrote:

> Richard Bonner wrote:

> > (For some reason no original post appeared on my serevr. As such, this
> > suggestion may not be valid for the purpose.)

> The original post was in microsoft.public.win2000.cmdprompt.admin
> The cross-post to alt.msdos.batch was inappropriate.

*** Odd though that the cross-post did not appear on my server.
Normally, they do.


> > One might use XSET 's "DRIVETEST" feature to determine if a parameter
> > is a valid drive....

> Thank you, but the question was about doing it in a BAT script. I was
> not looking for anything requiring an external executable helper app.

> - Rich

*** OK. I am a big fan of using whatever is available and can do the
job, but understand if some cannot, or choose not to, use after-market
utilities.

I hope you locate what you need.

Richard Bonner
http://www.chebucto.ca/~ak621/DOS/

0 new messages