Page 1 of 1

check characters

Posted: 21 Jul 2016 14:15
by batchcc
Hello everyone what I'm trying to do is use a sting of text and check each character to see if it is numeric + - * or / and set each character to be a variable. What I tried to do was use the string length to do this but I'm not sure how to set each character as a variable. Thanks everyone.
Here's what I've got

Code: Select all

for /l %%v in (1,1,%_strlen%) do (
for /f "delims=0123456789" %%i in ("%1") do set var=%%i
if defined %beforexx:~-1% (set by= non) else (set by= yes)
   if %%v==%_strlen% goto done
   
)
:done

Re: check characters

Posted: 21 Jul 2016 15:01
by aGerman
What is your final goal? That sounds all quite overelaborate to me.
Seems you want to check whether or not the passed argument is a valid equation ...

Code: Select all

@echo off &setlocal

set "equation=%~1"
setlocal EnableDelayedExpansion
echo(!equation!|findstr /x "[0-9*/+-]*" >nul || (
  echo Invalid character found.
  pause
  exit /b
)
endlocal

set /a "%equation%" 2>nul || (
  echo Equation can't be solved.
  pause
  exit /b
)

echo OK
pause

Re: check characters

Posted: 23 Jul 2016 05:03
by batchcc
Thanks aGerman but when I run the code I just get "Equation can't be solved."

Re: check characters

Posted: 23 Jul 2016 05:25
by aGerman
Did you pass an argument? As in your code I process %1 too.
For testing purposes you can assign variable "equation" directly with the string you want to validate.

Regards
aGerman