
Re: How to extract characters from a string
Starting with
yzip wrote:
Code:
set today=%date%
set mth=%today:~4,2%
set mths=janfebmaraprmayjunjulaugsepoctnovdec
SET /A posn=100%mth%%%100*3-3
... you have several options:
without delayed expansion
Code:
call set mmm=%%mths:~%posn%,3%%
The variables are expanded in two passes because of the CALL. After the first pass %posn% is expanded to the number and %%mths%% becomes %mths% which is expanded on the second pass.
with delayed expansion
Code:
setlocal enableDelayedExpansion
set mmm=!mths:~%posn%,3!
Either technique above is all you need for your example. But sometime you might find yourself in a position where you need both mths and posn to be expanded with delayed expansion (such as if posn were being set within a for loop.
The following will NOT work (assuming delayed expansion is already enabled)
Code:
set mmm=!mths:~!posn!,3!
It is much like your original dilemma.
The solution in this case is:
Code:
for %%n in (!posn!) do set mmm=!mths:~%%n,3!
One other point - expanding to a substring is not dependent on the set command. You can use the technique at any point. For example, if all you wanted to do was display the month without setting a variable, you could simply use:
Code:
call echo %%mths:~%posn%,3%%
Hope that helps
Dave Benham