1.

Solve : Batch script how to check a variable if all letters and/or numbers?

Answer»

I need to check a variable in my dos batch script if it contains all letter and/or all numbers.

If it's all letters, then PERFORM
If it's all numbers, then perform
If it's letters and numbers, then perform
OTHERWISE, error

Your help is greatly appreciated.
Thanks.Nothing is idiot proof in batch code and regular expressions are no exception. This little snippet demonstrates method.

Code: [Select]@echo off
setlocal
set /p var=Enter var:
echo %var%|findstr /r "[^0-9]" > nul
if ERRORLEVEL 1 actionX & goto tag
echo %var%|findstr /r "[^a-zA-Z]" > nul
if errorlevel 1 actionY & goto tag
echo %var%|findstr /r "[^0-9a-zA-Z]" > nul
if errorlevel 1 (actionZ) else echo error
:tag

The snippet requests a STRING entered at the console. You should be able to modify the logic and incorporate into your own script.

Good luck.

If any of the NT batch special characters exist in the string, the code will break.
Great! That worked.
Thanks.



Discussion

No Comment Found