Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I get the position of a character from a string in batch

I have a result string saved in a variable RES, this result is something like 2.3/5.0 I would like to get the part before the "/" and sending it to the batch output through an ECHO command. I have been searching how can I do this using batch commands, but only getting results of making substring to a fixed position, but how can I know this position? If i would knew the position the only thing I should probably do is:

ECHO %RES:~0,pos%

Then the question is: how I get this position from the string? Thanks

like image 972
yosbel Avatar asked Feb 20 '14 18:02

yosbel


2 Answers

If you really want the position itself, you can apply the strlen function from How do you get the string length in a batch file? to the result below, but if you just want that prefix, you can simply do:

set "res=2.3/5.0"
for /f "tokens=1 delims=/" %%i in ("%res%") do (set prefix=%%i)
like image 129
mbroshi Avatar answered Nov 12 '22 13:11

mbroshi


Why looking for the position of the delimiter instead of just using it?

for /f "delims=/" %%i in ("%RES%") do echo %%i
like image 36
Stephan Avatar answered Nov 12 '22 11:11

Stephan