Reputation: 27
I am trying to test if a string is uppercase. I know that this works:
@echo off
setlocal enabledelayedexpansion
set X=A
set Y=a
echo !X!|findstr "^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]*$"
echo !errorlevel!
echo !Y!|findstr "^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]*$"
echo !errorlevel!
and this results with an errorlevel
of 1 if it isn't uppercase, but I would like to find out if it is uppercase without the echo
portion, like using an if
statement. But I don't know how to use the findstr
and pass it a variable to test, so that it can result with an errorlevel
I can test in an if
statement.
Upvotes: 1
Views: 1993
Reputation: 517
@echo off
setlocal enabledelayedexpansion
rem this bat only validates one character.
set Y=a
for /f "delims=" %%i in ('echo !Y!^|findstr "^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]*$"') do set output=%%i
if [%output%]==[] (echo !Y! is lower case) else (echo !Y! is upper case)
rem Expected output: a is lower case
Because It is using findstr.
findstr/?
Searches for strings in files.
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
[/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
strings [[drive:][path]filename[ ...]]
If a new text file containing [a-z] is not created, the ECHO portion may be unavoidable. I slightly modify the code to avoid echo out unnecessary information.
Upvotes: 0
Reputation: 130889
You need ECHO, but you don't need to test ERRORLEVEL. You can use &&
to test if the preceding command failed, or ||
to test if it failed. You also can redirect FINDSTR to nul, since you don't need to see the output.
@echo off
setlocal enabledelayedexpansion
set X=A
set Y=a
for %%V in (X Y) do echo(!%%V!|findstr "^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]*$" >nul && (
echo !%%V! is all upper case
) || (
echo !%%V! is not all upper case
)
Everything could be put on one line, but I used parentheses and multiple lines for readability.
Upvotes: 3