how to check parameter

May 14, 2010 at 11:11:49
Specs: Windows 7
Part of my batch file is to accept an input

echo "Enter Asset Tag - xxxxxx-xxxx"
set /p AssetTag=

How can I confirm asset tag contain the format "xxxxxx-xxxx" and that the x are all numeric


See More: how to check parameter

Report •

May 14, 2010 at 14:51:15
Hi cdrandrew

@echo off
SetLocal EnableDelayedExpansion

echo "Enter Asset Tag - xxxxxx-xxxx"
set /p AssetTag=
echo %assetTag% > AssetTag.txt
call :StrLen %AssetTag%
findstr "^[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]*" AssetTag.txt
echo %ErrorLevel% %Len%
if %Len% NEQ 11 (
echo AssetTag Incorrect Length
Goto Again
if %ErrorLevel% NEQ 0 (
echo AssetTag Incorrect
goto Again
echo AssetTag %AssetTag% is OK
rem Put your code here on correct input
exit /b

:: Calc Var Length
:: %*=Str to Check
:: Returns %Len%
:: ---------------
:StrLen %*
set Data=%*
for /L %%a in (0,1,80) do (
set Char=!Data:~%%a,1!
if not "!Char!"=="" (
set /a Len=%%a+1
) else (exit /b)
exit /b

Report •

May 14, 2010 at 15:29:44
here's another offering (dtech beat me to the punch but i already wrote this so..
Dtech's findstr method an excellent alternative method. this would eliminate
the squib-file and the char.length test in his version:
echo %tag% | findstr "^[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$"

@echo off && setlocal enabledelayedexpansion
set bad=0
set /p tag=6 dash 4:
set test=%tag:~6,1%
if "%test%" neq "-" goto :err
set n1=%tag:~0,6%
call :numtest %n1% 5
set n1=%tag:~7%
call :numtest %n1% 3
if %bad% neq 0 goto :eof
:: proceed with ?

set /a bad+=1
echo bad entry
goto :eof

:: this not my idea, borrowed from Judago (nice technique to remember)
set test=
for /f "tokens=1 delims=0123456789" %%a in ("%1") do set test=%%a
if "%test%" neq "" goto err
:: end of Judago-code, begin my cruft
set aa=%1
set aa=!aa:~%2,1!
if "%aa%" equ "" goto :err
set /a zz=%2+1
set aa=!aa:~%zz%,1!
if "%aa%" neq "" goto :err
goto :eof

Report •

Related Solutions

Ask Question