中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-11 06:20
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » [Solved] A problem with nested variables
Printable Version  2,649 / 10
Floor1 hongewuyan Posted 2008-06-23 17:02
初级用户 Posts 29 Credits 65
Ask a question, there is a function I have always not known how to implement.

For example:

First set the following two variables:

set a=1

set b1=10

Then execute echo %b%a%% to get %b1%

In fact, what I want to get is the value assigned to b1, that is, 10

Ask everyone, can it be achieved?

[ Last edited by HAT on 2008-12-23 at 10:10 ]
Floor2 bat-zw Posted 2008-06-23 17:15
金牌会员 Posts 1,276 Credits 3,105
```
@echo off
set a=1&set b1=10
call,echo %%b%a%%%
pause>nul
```
Floor3 hongewuyan Posted 2008-06-23 17:33
初级用户 Posts 29 Credits 65
It turns out it can be achieved. Thanks to the expert upstairs. But there are still some doubts. What does "call" mean in the line "call,echo %%b%a%%%"? I can't do it directly by echoing "%%b%a%%%", but it works... I know that "call" is used to call other batch files or labels, and the comma behind it is confusing.
Floor4 lxmxn Posted 2008-06-23 22:07
版主 Posts 4,938 Credits 11,386
Originally posted by hongewuyan at 2008-6-23 17:33:
It really works.
Thanks to the expert above.

But there are still some doubts.

call,echo %%b%a%%%

Can you explain this line? What does call mean?
I can't do it directly with echo %%b%a%%%, but it works when adding call. ...

Here, call actually reorganizes and expands the command line. First, expand %a% inside %%b%a%%% so that %a% becomes the value 1 of a, and then use cal to expand %b1%.

It can also be implemented using variable delay. The method is as follows:


The usage of call here is actually a quick way of variable delay. Variable delay is generally used in the loop body of for.

In call,%%b%a%%%, the comma here is actually a separator, the same as a space. There are many separators available. For example, in the above example, echo:!b%a%! ,Of course, not all commands can be used in this way, depending on the situation...
C:\>echo+cn-dos.net
cn-dos.net

C:\>echo/cn-dos.net
cn-dos.net

C:\>echo






Floor5 pusofalse Posted 2008-06-23 22:17
银牌会员 Posts 646 Credits 1,604
My understanding is that if there are still variables within variables, you need to use variable delay call delay or setlocal to enable delay. I wonder if this understanding is correct...
Floor6 hongewuyan Posted 2008-06-24 11:24
初级用户 Posts 29 Credits 65
Thanks everyone for your help, I really understand this time...
Floor7 mysuntjy Posted 2008-12-23 07:13
新手上路 Posts 10 Credits 13 From 重庆市
Originally posted by bat-zw at 2008-6-23 17:15:
Why does adding a space on both sides of the & connecting the two set statements, that is: set a=1 & set b1=10, result in "ECHO is off"?
Floor8 lxmxn Posted 2008-12-23 09:48
版主 Posts 4,938 Credits 11,386
Originally posted by mysuntjy at 2008-12-23 07:13:

Why does adding a space on both sides of & connecting two set statements, that is: set a=1 & set b1=10, the running result is: ECHO is off.

????

After adding the space, the value of variable a is 1 plus a space, not the original single 1.
Floor9 95167958 Posted 2008-12-23 12:23
新手上路 Posts 1 Credits 1
In the statement `call,echo %%b%a%%`, the extra `%` signs are related to how batch processing in Windows handles variable expansion. In a batch file, when you want to literally output a `%` character, you need to use `%%` because the single `%` is used for variable substitution. Let's break it down:

The part `%%b%a%%` - the first `%%` is to output a single `%` before the variable `b`, then `%a%` is the variable `a` with the surrounding `%` signs, and the last `%%` is to output a single `%` after. If you just wrote `call,echo %b%a%%`, there would be issues with how the batch parser interprets the variable expansion and the literal `%` characters. So the extra `%` signs are there to correctly handle the literal `%` output and proper variable expansion in the batch processing context.
Floor10 HAT Posted 2008-12-23 13:14
版主 Posts 5,017 Credits 9,023
Floor11 gool123456 Posted 2010-05-27 17:28
初级用户 Posts 76 Credits 89
Haha, suddenly I see the light.
A good post shouldn't sink, top it to let more people see!
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023