Reputation: 37
Here's a snippet of my file:
set checkVal=0
:checkforStart
call set checkSub=%%_input:~%checkVal%,1%%
if /i %checkSub% EQU ( goto parenthesisCheck
if /i %checkSub% EQU ' goto noPar
set /a checkVal=%checkVal% + 1
goto checkforStart
:parenthesisCheck
set /a checkVal=%checkVal% + 1
set _startPar=%checkVal%
:checkforEnd
call set checkSub=%%_input:~%checkVal%,1%%
if /i %checkSub% EQU ) goto endParenthesis
if /i %checkSub% EQU ( goto parenthesisCheck
set /a checkVal=%checkVal% + 1
goto checkforEnd
:endParenthesis
set /a _endPar= %checkVal% - %_startPar%
call set parContents=%%_input=:~%_startPar%,%_endPar%%%
echo Parenthesis: %parContents%
:noPar
pause >nul
goto end
The problem I'm having is that when I try to return what was in the parenthesis, it returns:
~1,4
Or something along those lines, instead of the numbers or letters that are supposed to be isolated from the original input.
Thank you for any and all help.
Here is the full file, if you need it: https://www.dropbox.com/s/2lljcy5t7qme0nb/substring%20parenthesis%20finder.txt
Upvotes: 1
Views: 79
Reputation: 130889
That is the value you will get if you attempt to do something like set val=%var:~1,4%
when var is undefined. Substring and search/replace operations do not work properly if the variable is undefined - instead they return the instructions after the colon. This is exactly the situation you have because your final assignment has an unwanted =
in the expression.
You have:
call set parContents=%%_input=:~%_startPar%,%_endPar%%%
So it is looking to extract a substring from a variable named _input=
, which cannot possibly exist - user defined variable names cannot contain =
.
Easily fixed by removing the =
call set parContents=%%_input:~%_startPar%,%_endPar%%%
Upvotes: 3
Reputation: 37
It seems that a line that used &&
that I did not post on here was causing the problem. Sorry for the trouble.
Upvotes: 0
Reputation: 80123
....
:endParenthesis
set /a _endPar= %checkVal% - %_startPar%
CALL :inpar %_startPar% %_endPar%
echo Parenthesis: %parContents%
:noPar
GOTO :EOF
:inpar
CALL set parContents=%%_input:~%1,%2%%
GOTO :eof
This worked for me...
Upvotes: 0
Reputation: 5207
You should instead enable delayed expansion at the start of your script and use !
in place of the surrounding %
symbols.
setLocal enableDelayedExpansion
set checkSub=!_input:~%checkVal%,1!
set parContents=!_input=:~%_startPar%,%_endPar%!
Note, you set the checkSub
variable twice, it's exactly the same both times, so I didn't include it twice. There are three lines you need to change, and one to add.
Upvotes: 0