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-25 06:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Ask about the `set /a` issue in the `for /l` statement View 2,341 Replies 16
Original Poster Posted 2007-03-09 23:59 ·  中国 浙江 绍兴 电信
高级用户
★★★
Autowalk
Credits 845
Posts 375
Joined 2007-03-03 01:58
19-year member
UID 80606
Gender Male
Status Offline
```
echo off
set a=0
for /l %%i in (71,1,99) do set /a a+=1 & echo %a%

The %a% here always shows 0. It seems that set /a a+=1 doesn't work at all. What's the reason for this? Is it that set /a can't be used in for /l?
```
Floor 2 Posted 2007-03-10 00:17 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
LZ didn't understand the usage

@echo off
set a=0
for /l %%i in (71,1,99) do set /a a+=1
echo %a%
pause

If you want to use it on the same line, you must enable delayed variables (specifically search the forum)
Floor 3 Posted 2007-03-10 01:41 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
@echo off
set a=0
for /l %%i in (71,1,99) do set /a a+=1 & call echo %%a%%

pause>nul
Floor 4 Posted 2007-03-11 01:54 ·  中国 江苏 连云港 电信
高级用户
★★★
前进者
Credits 641
Posts 303
Joined 2007-01-10 02:57
19-year member
UID 76009
Gender Male
Status Offline
I also have a place I don't understand. What does "set /a a+=1" mean? The key is what the usage of a+=1 is. Which friend who knows tells me, thank you.
我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿!
Floor 5 Posted 2007-03-11 02:00 ·  中国 浙江 绍兴 诸暨市 电信
高级用户
★★★
Autowalk
Credits 845
Posts 375
Joined 2007-03-03 01:58
19-year member
UID 80606
Gender Male
Status Offline
The `set /a a+=1` is equivalent to `set /a a=%a%+1` in the context of batch scripting in Windows. So the translation is: `set /a a+=1` means equal to `set /a a=%a%+1`
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
oilio +2 2007-03-11 09:21
Floor 6 Posted 2007-03-11 08:19 ·  中国 江苏 连云港 联通
高级用户
★★★
前进者
Credits 641
Posts 303
Joined 2007-01-10 02:57
19-year member
UID 76009
Gender Male
Status Offline
Thank you, friend from the upper floor.
我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿!
Floor 7 Posted 2007-03-11 08:34 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by oilio at 2007-3-10 12:54:
I also have a question I don't understand. What does "set /a a+=1" mean? The key is what the usage of a+=1 is for? Friends who know, please tell me, thank you.

Friends who have learned other programming languages may know this usage at a glance. There are many similar usages, such as: set /a a*=2, set /a a-=5, etc., which respectively mean set /a a=a*2, set /a a=a-5. Generally, set /a a+=1 is called an accumulator. Each time it runs, the value of a increases by 1, which is mainly used to control the program flow.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
oilio +4 2007-03-11 09:14
Floor 8 Posted 2007-03-11 09:21 ·  中国 江苏 连云港 联通
高级用户
★★★
前进者
Credits 641
Posts 303
Joined 2007-01-10 02:57
19-year member
UID 76009
Gender Male
Status Offline
Thanks, Brother lxmxn. I really don't know what to say, thanks! This is the Nth time I've seen this usage. Because I used to be very weak, now my level is a little better than before. Today, when I saw this usage again, I thought it was time, and couldn't help but want to ask. Thanks everyone, especially Brother lxmxn for listing several examples. Brother lxmxn mentioned the accumulator. I think that in this batch processing, using set /a a+=1, the main function is to calculate that from 71 with a step value of 1 increasing to 99, a total of 28 increments occur. But to get the total number of numbers, the number of times does not include 71 itself, so when calculating the total number of numbers, one is less. So here set /a a+=1 is used. I don't know if my understanding is correct? It seems that I made a gain today, and I understand two things at once. I'll practice it to deepen my impression. Thanks everyone again.

[ Last edited by oilio on 2007-3-10 at 09:14 PM ]
我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿!
Floor 9 Posted 2007-03-11 09:37 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by oilio at 2007-3-10 20:21:
Thank you, brother lxmxn. I really don't know what to say, thank you! This is the Nth time I've seen this usage. Because I've been very weak before, and now my level is a little better than before. Today, when I see this usage again, I feel that...

Brother, there's no need to be so polite. Everyone in the forum is also a fate. Let's learn and progress together.

I'm really happy to see that you've made great progress these days.
Floor 10 Posted 2007-03-11 10:03 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by oilio at 2007-3-10 20:21:
I think that the main function of using set /a a+=1 in this batch processing is to calculate that from step size 1 increasing from 77 to 99, a total of 28 increments occur, but to get the total number of numbers, the count does not include 71 itself, so when calculating the total number of numbers, it will be one less. So here set /a a+=1 is used. I don't know if my understanding is correct?

Brother's understanding is a bit deviated. Here, there are a total of 99-71+1=29 increments, because the for /l loop includes the first number, which is 71. You can try for /l %a in (1,1,29) do, and the result should be the same. The function of set /a a+=1 is not to increase the increment by one, but to make the value of a increase by 1 each time the loop is executed. Here, the loop is executed 29 times, so the final result should be 29.
Floor 11 Posted 2007-03-11 10:13 ·  中国 江苏 连云港 联通
高级用户
★★★
前进者
Credits 641
Posts 303
Joined 2007-01-10 02:57
19-year member
UID 76009
Gender Male
Status Offline
It seems incorrect. Let me think. If it's for /l %a in (1,1,3), from 1 to 2 is an increase of 1 time, and from 2 to 3 is the second increase. There are a total of 2 times. But there are actually three numbers here. Shouldn't the number of increments be 2 times? This command should be calculating the number from 1 to 3. I don't know if my understanding is wrong, heh heh. Sometimes I feel quite smart, but sometimes I get stuck, heh heh. Tired, going to sleep. I'll think about it again. Maybe I can figure it out after resting, heh heh. Thanks to brother lxxm for the reply, which has benefited me a lot.
我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿!
Floor 12 Posted 2007-03-11 10:15 ·  中国 浙江 绍兴 诸暨市 电信
高级用户
★★★
Autowalk
Credits 845
Posts 375
Joined 2007-03-03 01:58
19-year member
UID 80606
Gender Male
Status Offline
for /l %a in (1,1,3) do
When %a=1, count once
When %a=2, count once
When %a=3, count once
So it should be 3 times
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
oilio +3 2007-03-11 10:17
Floor 13 Posted 2007-03-11 10:19 ·  中国 江苏 连云港 联通
高级用户
★★★
前进者
Credits 641
Posts 303
Joined 2007-01-10 02:57
19-year member
UID 76009
Gender Male
Status Offline
Thanks, Brother Zhou Shijie. So my problem was here. The key is that I mentioned incrementing and forgot about this. If I start counting from the first number, that's right. I confused myself. Haha. Thanks.
我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿!
Floor 14 Posted 2007-03-15 11:14 ·  中国 湖北 黄冈 电信
高级用户
★★★
Credits 894
Posts 411
Joined 2007-02-17 12:15
19-year member
UID 79697
Gender Male
Status Offline
I tried many times before, and I couldn't use it on the same line. It turns out that using variable delay can do it. How exactly to use it?
@set c= 不知则觉多,知则觉少,越知越多,便觉越来越少. --- 知多少.
@for,/l,%%i,in,(1,1,55)do,@call,set/p=%%c:~%%i,1%%<nul&ping/n 1 127.1>nul


Floor 15 Posted 2007-03-15 11:38 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Here are two ways to use variable delay:

1. Enable variable delay:

@echo off
setlocal EnableDelayedExpansion
set a=0
for /l %%i in (71,1,99) do set /a a+=1&echo !a!
pause>nul

2. Use call

@echo off
set a=0
for /l %%i in (71,1,99) do set /a a+=1&call echo %%a%%
pause>nul
Forum Jump: