check characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

check characters

#1 Post by batchcc » 21 Jul 2016 14:15

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: check characters

#2 Post by aGerman » 21 Jul 2016 15:01

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

batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Re: check characters

#3 Post by batchcc » 23 Jul 2016 05:03

Thanks aGerman but when I run the code I just get "Equation can't be solved."

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: check characters

#4 Post by aGerman » 23 Jul 2016 05:25

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

Post Reply