0

I'm trying to write a script that prompts the user for a drive letter and then validate the entry insofar that it is a single character and falls within a valid range: D-Z

This is what I have so far:

setlocal enableextensions enabledelayedexpansion
set MIN_DRIVE_LOWER=d
set MAX_DRIVE_LOWER=z
set /P RELEASE_DRIVE="What is the assigned network drive letter on this workstation? "
echo Release drive is %RELEASE_DRIVE%
if /I !RELEASE_DRIVE! GEQ %MIN_DRIVE_LOWER% (
  if /I !RELEASE_DRIVE! LEQ %MAX_DRIVE_LOWER% (
    echo Release drive is lower-case.
  )
)

No matter what I enter at the prompt my script reaches the "echo Release drive is lower-case." Any thoughts or suggestions would be much appreciated.

4
  • are you sure, you want that /I? Commented Aug 6, 2015 at 19:40
  • @Stephan I don't know how to fix this. That's why I'm posting here. Commented Aug 6, 2015 at 19:46
  • when I interpret your code right, you want to test, if the user did input an upper- or lowercase letter. /i tells if to ignore capitalization. Commented Aug 6, 2015 at 19:57
  • @Stephan That's just part of it. In the end, I want to validate the input by verifying that the user entered only a single character, D-Z. If they entered a valid lower-case character, I want to just convert it to upper-case. Commented Aug 6, 2015 at 20:40

2 Answers 2

2

Here's a hacksy alternative. Rather than giving the user the freedom to screw up then punishing him for his insolence, you can enforce entry of a valid response with the choice command. Then to avoid a long list of if errorlevel conditions to convert the choice back to its alpha value, there's a trick using a special environment variable that converts ERRORLEVEL to an alphanumeric character, %=ExitCodeAscii%. More details are here.

@echo off
setlocal

choice /c defghijklmnopqrstuvwxyz /n /m "What is the assigned network drive letter on this workstation? "
set /a ascii = %ERRORLEVEL% + 67
cmd /c exit /b %ascii%
set "letter=%=ExitCodeAscii%"

echo You chose %letter%

It's not clear what your goal is, whether the drive letter you want chosen is an existing letter or an unused letter for a drive mapping. Depending on your intentions, you might be interested in one or both of the following tips. *shrug*

You can programmatically determine what drive letters are currently assigned with a WMI query using wmic:

wmic logicaldisk get deviceid /value | find "="

If you want to create a network drive mapping and would like to allow Windows to figure out which drive letter to assign automatically, use pushd.

net use \\server /user:domain\username password
pushd \\server\share

... then to remove the mapping:

popd
net use \\server /delete
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Thanks for the ninja tip!
1

Why don't you try this:

@echo off
setlocal enableextensions enabledelayedexpansion
set /P rd="What is the assigned network drive letter on this workstation? "
echo Release drive is %rd%

:: Variable valid will be either 0 or 1
set valid=0
for %%a in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
  if /i "%rd%"=="%%a" (
    set valid=1
    if "%rd%" neq "%%a" set rd=%%a
  )
)
Echo.

if "%valid%"=="1" (
Echo Valid Input Detected: %rd%
) else (
Echo Invalid Input: %rd%
)
pause

Edit: I made it so that it converted the character to upper-case too.

2 Comments

How would the line if "%rd%" neq "%%a" set rd=%%a make any difference, since it's in the scope of the if statement two lines above it?
@JimFell: for conversion to upper case. It's parsed (for example) as: if "d" neq "D" set rd=D. A bit redundant, just set rd=%%A would do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.