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-07-31 17:47
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » It can also be calculated without using variable delay [setlocal enableDelayedExpansion] View 4,771 Replies 49
Original Poster Posted 2008-06-24 22:20 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
Maybe everyone knows, for those who don't know, let's learn.

It can also be used in other places.
::Without variable delay  can also calculate
@echo off
for /f %%a in ('dir/b') do call :num_add file_num
echo There are %file_num% files (folders) in the directory.
pause
goto :eof

:num_add
 set/a %1+=1
 goto :eof
┌───────┐
├→学习→实践→┤
└───────┘
Floor 2 Posted 2008-06-24 22:36 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
I don't know, learning.
Floor 3 Posted 2008-06-24 22:55 ·  中国 湖北 武汉 联通
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Originally posted by HAT at 2008-6-24 22:36:
Don't know, learning.

Orz ……
Floor 4 Posted 2008-06-24 22:56 ·  法国
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
This is also the role of call

(for /f %i in ('dir /b') do set /a n+=1 >nul)&call echo There are %n% files(folders) under the directory
菩提本无树,明镜亦非台,本来无一物,何处惹尘埃.
Floor 5 Posted 2008-06-25 18:59 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
The usage seen on the 4th floor is another new one I've encountered.
┌───────┐
├→学习→实践→┤
└───────┘
Floor 6 Posted 2008-06-25 19:03 ·  法国
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
This is no different from the original poster's, just simplified a bit
菩提本无树,明镜亦非台,本来无一物,何处惹尘埃.
Floor 7 Posted 2008-06-25 19:07 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
20-year member
UID 59307
Status Offline
But I'm a newbie, and I find such usage a bit strange and not very used to it. Still, I need to learn more from you all.
┌───────┐
├→学习→实践→┤
└───────┘
Floor 8 Posted 2008-06-25 19:07 ·  中国 广东 韶关 电信
高级用户
★★★
CMD感染者
Credits 691
Posts 383
Joined 2008-05-23 00:38
18-year member
UID 119451
Gender Male
Status Offline
Floor 9 Posted 2008-06-25 19:22 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 195
Posts 93
Joined 2006-10-28 08:20
19-year member
UID 68626
Gender Male
Status Offline
This kind of thing can't replace the delayed variable. They all display the final effect after completely ending a for + set loop, and can't use intermediate links at all. The role of the delayed variable is to use the intermediate results of set.

@echo off
for /f %%a in ('dir/b') do (
call :num_add file_num
echo %file_num%
)
echo There are %file_num% files (folders) in the directory.
pause
goto :eof

:num_add
 set/a %1+=1
 goto :eof


@echo off
for /f %%i in ('dir /b') do (
set /a n+=1 >nul
echo %n%
)
echo There are %n% files (folders) in the directory
pause


The above two are the same as the building owner and the above ppdos code, and can't use intermediate results.

@echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir /b') do (
set /a n+=1 >nul
echo !n!
)
echo %n%
pause


You can compare the results.

[ Last edited by metoo on 2008-6-25 at 07:26 PM ]
Floor 10 Posted 2008-06-25 19:51 ·  法国
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
If you want to use the intermediate result, it's also okay.

(for /f "tokens=*" %i in ('dir /b') do set /a n+=1 >nul&call set _%n%=%i >nul)&call echo There are %n% files(folders) in the directory



echo %_9%


[ Last edited by PPdos on 2008-6-25 at 01:01 PM ]
菩提本无树,明镜亦非台,本来无一物,何处惹尘埃.
Floor 11 Posted 2008-06-25 20:40 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 195
Posts 93
Joined 2006-10-28 08:20
19-year member
UID 68626
Gender Male
Status Offline
This is a clever way. It uses consecutive multi-variable assignments respectively to obtain the value of one of the intermediate variables, rather than directly obtaining it through true consecutive assignment/intermediate links. It defines n variables to stepize the entire for loop, and these stepized results can only be used after the entire loop ends.

[ Last edited by metoo on 2008-6-25 at 08:42 PM ]
Floor 12 Posted 2008-06-25 20:49 ·  法国
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
Originally posted by metoo at 2008-6-25 01:40 PM:
This is a clever way, which uses continuous multi-variable assignment respectively to obtain the value of one of the intermediate variables, rather than directly obtaining it through continuous assignment/intermediate links. It is to define n variables to...


Can the expert give an example?
菩提本无树,明镜亦非台,本来无一物,何处惹尘埃.
Floor 13 Posted 2008-06-25 21:09 ·  中国 山西 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Have everyone been talking for a long time and not realized that they are discussing around a very non-typical example? Doesn't anyone know that set /a has the default delay effect? The example of the LZ completely doesn't need variable delay or "intermediate results" and so on.

@echo off
for /f %%a in ('dir/b') do set /a file_num+=1
echo There are %file_num% files (folders) under the directory %cd%.
Floor 14 Posted 2008-06-25 21:12 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 195
Posts 93
Joined 2006-10-28 08:20
19-year member
UID 68626
Gender Male
Status Offline
- Just a beginner

Your approach is to obtain each step by "changing variables", which is to generate n different variables at one time. These variables can only be used after the for command has completely executed. This is what I call taking a shortcut. While delayed expansion allows using variables during the execution of the for command.

@echo off
for /f "tokens=*" %%i in ('dir /b') do (
set /a n+=1 >nul
call set _%n%=%%i >nul
rem This sentence is actually problematic. Before the for is completed, %n% is empty, so all are setting the value of the _ variable
echo %n%
echo %_%
echo %_1%
rem The above 3 sentences are added by myself to verify whether the variables in the loop can be used
)
echo\
echo\
echo\
::New line
::By this point, the entire loop has ended, and the variables %n% and %_% take effect
echo %_%
echo %n%
echo %_1%
::Verify that the %_1% variable does not exist
echo\
echo\
echo\
::New line
::The above 3 sentences are added by myself to verify whether the variables in the loop can be used
call echo There are %n% files(folders) in the directory
pause


[ Last edited by metoo on 2008-6-25 at 09:14 PM ]
Floor 15 Posted 2008-06-25 21:15 ·  中国 山西 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Brother metoo's remarks on floor 9 are a bit thoughtless.

In the DOS era without variable delay, it is also possible to "assign continuously / directly obtain in the intermediate link".

This only needs to make some small changes to the :num_add section that is called.
And the scheme of using for + call instead of for + delay usually transfers all the loop body code to the called subroutine, and there are no restrictions inside the subroutine, where you can use it as you like.
Forum Jump: