Extracting substring from a batch or function argument
Posted: 25 May 2016 21:22
On SS64 Cmd Syntax Replace page this syntax is given but not explained or illustrated with examples:
A.
It appears to hint how a substring can be extracted from a batch or function argument. Is that correct?
One way to extract such substring is to assign a variable to a batch or function argument, then extract substring of the variable:
But it requires changing the variable name. I need to set "opt=5" (i.e. change a value of the "hidden" variable in argument %1), but instead the above substitution only seems to allow setting "var2=5". How would using syntax (A) above may help, or is there any other short way to extract a substring without changing variable name, like ECHO or PIPE?
A.
Code: Select all
%~[param_ext]$variable:Param
It appears to hint how a substring can be extracted from a batch or function argument. Is that correct?
One way to extract such substring is to assign a variable to a batch or function argument, then extract substring of the variable:
Code: Select all
:: if argument %1="%opt%"
set "var1=%1"
set "var2=%var1:~2,3%"
But it requires changing the variable name. I need to set "opt=5" (i.e. change a value of the "hidden" variable in argument %1), but instead the above substitution only seems to allow setting "var2=5". How would using syntax (A) above may help, or is there any other short way to extract a substring without changing variable name, like ECHO or PIPE?