I was assigned to understand the code that was written by some other person so far I could understood to some extent but one line is causing me trouble
call set mmm=%%mon:~%id%,3%%To the extent I understandcall is used to initiate anotherbatch file but in the above line there was variable set to some value but I am not sure what does that line does.
Also how does%% is used here as far as I know% is used to retrive the value from a variable.
Complete code is here:
set mon=JANFEBset id=123call set mmm=%%mon:~%id%,3%%2 Answers2
It's a two-stage substitution, allowing you to usevariables within the interpretation. Normally, with an expression like:
%str:~start,len%thestart andlen must be numeric constants rather than variables. So this is the way you use a variable instead of a constant.
Starting with:
set mmm=%%mon:~%id%,3%%In the first stage,%% markers are replaced with% and%id% is replaced with123, giving:
set mmm=%mon:~123,3%In the second stage, it's interpreted as you would expect, although the offset123 seems a little strange in this case.
You can see the effect here:
@setlocal enableextensions enabledelayedexpansion@echo offrem 1 2 3 4rem 01234567890123456789012345678901234567890123456set mon=JAN.FEB.MAR.APR.MAY.JUN.JUL.AUG.SEP.OCT.NOV.DECrem ^rem 28set id=28call set mmm=%%mon:~%id%,3%%echo %mmm%endlocalwhich outputsAUG.
Two steps:
first, the value ofid is substituted into the expression, so it becomesset mmm=%%mon:~123,3%%
Then the expression is evaluated - as a single-line "subroutine", removing one level of% :set mmm=%mon:~123,3%
so, attempts to setmmm to the 3 characters following the 1323rd character inmon (counting from "character 0")
Which should "set"mon tonothing
(ifid was3, thenmmm would be set toFeb)
Comments
Explore related questions
See similar questions with these tags.



