0

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%%
halfer's user avatar
halfer
20.2k20 gold badges110 silver badges207 bronze badges
askedJan 22, 2015 at 9:01
Siva's user avatar

2 Answers2

4

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%endlocal

which outputsAUG.

answeredJan 22, 2015 at 9:06
paxdiablo's user avatar
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response.. if%% is interpreted as% then what is the purpose of using%% instead we can only use%. How doesCall work here?
@Siva,call introduces the second stage. The reason you need%% is because that becomes% during the first stage.
1

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)

answeredJan 22, 2015 at 9:09
Magoo's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.