Checking whether parameter is a drive letter

  • Thread starter Thread starter Rich Pasco
  • Start date Start date
R

Rich Pasco

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 said:
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: )?

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
 
Rich Pasco said:
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,

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) said:
Rich Pasco said:
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,

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

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

fsutil fsinfo drives | findstr /i "F:" && echo Valid. || echo Invalid.
 
Dean said:
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
 
Rich Pasco said:
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

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.
)
 
Dean Wells (MVP) said:
Rich Pasco said:
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

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.
)

--
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

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
 
Al said:
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
 
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)
 
Rich Pasco said:
Hi Al, I like it. It is the same method as I used in my own example,
my second post in this thread.

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

/Al
 
Esra said:
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)

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
 
(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/
 
foxidrive said:
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%" (
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
 
Richard said:
(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
 
Rich said:
Richard Bonner wrote:
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.

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.

*** 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/
 
Back
Top