China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-22 09:21
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] A problem with nested variables View 2,550 Replies 10
Original Poster Posted 2008-06-23 17:02 ·  IANA 局域网IP(Private-Use)
初级用户
Credits 65
Posts 29
Joined 2008-03-24 12:17
18-year member
UID 113853
Gender Male
Status Offline
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 ]
Floor 2 Posted 2008-06-23 17:15 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
```
@echo off
set a=1&set b1=10
call,echo %%b%a%%%
pause>nul
```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
gool123456 +2 2010-05-27 17:33
批处理之家新域名:www.bathome.net
Floor 3 Posted 2008-06-23 17:33 ·  IANA 局域网IP(Private-Use)
初级用户
Credits 65
Posts 29
Joined 2008-03-24 12:17
18-year member
UID 113853
Gender Male
Status Offline
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.
Floor 4 Posted 2008-06-23 22:07 ·  中国 湖北 武汉 联通
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
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:
@echo off

set /a a=1,b1=10

Setlocal EnableDelayedExpansion

echo:!b%a%! ...

pause


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






Floor 5 Posted 2008-06-23 22:17 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
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...
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 6 Posted 2008-06-24 11:24 ·  IANA 局域网IP(Private-Use)
初级用户
Credits 65
Posts 29
Joined 2008-03-24 12:17
18-year member
UID 113853
Gender Male
Status Offline
Thanks everyone for your help, I really understand this time...
Floor 7 Posted 2008-12-23 07:13 ·  中国 重庆 沙坪坝区 电信
新手上路
Credits 13
Posts 10
Joined 2008-11-13 00:09
17-year member
UID 130805
Gender Male
From 重庆市
Status Offline
Originally posted by bat-zw at 2008-6-23 17:15:

@echo off
set a=1&set b1=10
call,echo %%b%a%%%
pause>nul
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"?
Floor 8 Posted 2008-12-23 09:48 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
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.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
gool123456 +1 2010-05-27 17:36
Floor 9 Posted 2008-12-23 12:23
新手上路
Credits 1
Posts 1
Joined 2008-12-22 14:29
17-year member
UID 134612
Gender Male
Status Offline
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.
Floor 10 Posted 2008-12-23 13:14 ·  中国 上海 联通
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Floor 11 Posted 2010-05-27 17:28 ·  中国 广东 广州 电信
初级用户
★★
Credits 89
Posts 76
Joined 2009-12-13 13:41
16-year member
UID 156499
Gender Male
Status Offline
Haha, suddenly I see the light.
A good post shouldn't sink, top it to let more people see!
Forum Jump: