Be careful - you probably don't want a space in your variable name, so you need to remove the space before the =
in your SET statement.
One solution is to replace all the hex digits with nothing and see if you have anything remaining.
@echo off
setlocal enableDelayedExpansion
:Get_Hex
set "_HexVal="
set /p _HexVal=Enter hex value:
if not defined _HexVal echo You must enter a value. Try again.&goto Get_Hex
set "test=!_HexVal!"
for %%C in (0 1 2 3 4 5 6 7 8 9 A B C D E F) do if defined test set "test=!test:%%C=!"
if defined test echo Invalid input. Try again.&goto Get_Hex
I also added an additional test to make sure that something was entered. If nothing was entered, then the current value remains. That is why the variable must be cleared before the input statement.
A simpler method is to use FINDSTR to look for a line exclusively containing 1 or more hex digit characters:
@echo off
setlocal enableDelayedExpansion
:Get_Hex
set "_HexVal="
set /p "_HexVal=Enter hex value: "
echo !_HexVal!|findstr /ri "^[0123456789ABCDEF][0123456789ABCDEF]*$" >nul || (
echo Invalid input. Try again.
goto Get_Hex
)
Or you could use FINDSTR to look for non-hex digits, but then you must verify there is a value again.
@echo off
setlocal enableDelayedExpansion
:Get_Hex
set "_HexVal="
set /p "_HexVal=Enter hex value: "
if not defined _HexVal echo Invalid input. Try again.&goto Get_Hex
echo !_HexVal!|findstr /ri "[^0123456789ABCDEF]" >nul && (
echo Invalid input. Try again.
goto Get_Hex
)
Yet another method is to let FOR /F look for non-hex digits. Again you must also verify there is a value.
@echo off
setlocal enableDelayedExpansion
:Get_Hex
set "_HexVal="
set /p "_HexVal=Enter hex value: "
if not defined _HexVal echo Invalid input. Try again.&goto Get_Hex
for /f "eol=0 delims=0123456789ABCDEFabcdef" %%A in ("!_HexVal!") do (
echo Invalid input. Try again.
goto Get_Hex
)