|
wxcute
中级用户
  
积分 458
发帖 211
注册 2006-7-26
状态 离线
|
『楼 主』:
不用变量延迟[setlocal enableDelayedExpansion]也可以计算
使用 LLM 解释/回答一下
可能大家都知道,供还不知道的人学习下吧。
也可以用在其他地方。
::不用变量延迟[setlocal enableDelayedExpansion]也可以计算
@echo off
for /f %%a in ('dir/b') do call :num_add file_num
echo 目录下有%file_num%个文件(夹)。
pause
goto :eof
:num_add
set/a %1+=1
goto :eof
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
|

┌───────┐
├→学习→实践→┤
└───────┘ |
|
2008-6-24 22:20 |
|
|
HAT
版主
       
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
|
2008-6-24 22:36 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
Originally posted by HAT at 2008-6-24 22:36:
不知道,学习。
Orz ……
Originally posted by HAT at 2008-6-24 22:36:
Don't know, learning.
Orz ……
|
|
2008-6-24 22:55 |
|
|
PPdos
高级用户
   
积分 783
发帖 268
注册 2006-12-26
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
这也是call的作用所在
(for /f %i in ('dir /b') do set /a n+=1 >nul)&call echo 目录下有%n%个文件(夹)
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
|

菩提本无树,明镜亦非台,本来无一物,何处惹尘埃. |
|
2008-6-24 22:56 |
|
|
wxcute
中级用户
  
积分 458
发帖 211
注册 2006-7-26
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
4楼的又是我见的一种新用法。
The usage seen on the 4th floor is another new one I've encountered.
|

┌───────┐
├→学习→实践→┤
└───────┘ |
|
2008-6-25 18:59 |
|
|
PPdos
高级用户
   
积分 783
发帖 268
注册 2006-12-26
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
这跟楼主的没有什么区别 只是简化了一下
This is no different from the original poster's, just simplified a bit
|

菩提本无树,明镜亦非台,本来无一物,何处惹尘埃. |
|
2008-6-25 19:03 |
|
|
wxcute
中级用户
  
积分 458
发帖 211
注册 2006-7-26
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
不过我是新手,对这样的用法觉得有点奇怪,也不是很习惯。
还是要多与各位学习阿。
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.
|

┌───────┐
├→学习→实践→┤
└───────┘ |
|
2008-6-25 19:07 |
|
|
radem
高级用户
    CMD感染者
积分 691
发帖 383
注册 2008-5-23
状态 离线
|
|
2008-6-25 19:07 |
|
|
metoo
初级用户
 
积分 195
发帖 93
注册 2006-10-28
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
这种都不能代替延迟变量。都是在完全结束一个for+set循环后显示最终效果,根本不能使用中间环节,而延迟变量的作用就是使用set的中间结果
@echo off
for /f %%a in ('dir/b') do (
call :num_add file_num
echo %file_num%
)
echo 目录下有%file_num%个文件(夹)。
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 目录下有%n%个文件(夹)
pause
上边两个和楼主和上面ppdos的代码效果是一样的不能使用中间结果
@echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir /b') do (
set /a n+=1 >nul
echo !n!
)
echo %n%
pause
可以比较一下结果
Last edited by metoo on 2008-6-25 at 07:26 PM ]
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 ]
|
|
2008-6-25 19:22 |
|
|
PPdos
高级用户
   
积分 783
发帖 268
注册 2006-12-26
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
如果想用中间的结果 也是可以的
(for /f "tokens=*" %i in ('dir /b') do set /a n+=1 >nul&call set _%n%=%i >nul)&call echo 目录下有%n%个文件(夹)
echo %_9%
Last edited by PPdos on 2008-6-25 at 01:01 PM ]
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 ]
|

菩提本无树,明镜亦非台,本来无一物,何处惹尘埃. |
|
2008-6-25 19:51 |
|
|
metoo
初级用户
 
积分 195
发帖 93
注册 2006-10-28
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
这个是一个取巧的办法了,是用连续多变量分别赋值达到获得其中某个中间变量的值,而不是真正的连续赋值/中间环节直接获取。是定义了n个变量来把整个for循环分步化,而这些分布结果是必须在整个循环结束后才能使用的。
Last edited by metoo on 2008-6-25 at 08:42 PM ]
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 ]
|
|
2008-6-25 20:40 |
|
|
PPdos
高级用户
   
积分 783
发帖 268
注册 2006-12-26
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
Originally posted by metoo at 2008-6-25 01:40 PM:
这个是一个取巧的办法了,是用连续多变量分别赋值达到获得其中某个中间变量的值,而不是真正的连续赋值/中间环节直接获取。是定义了n个变量来 ...
大侠能否举个例子?
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?
|

菩提本无树,明镜亦非台,本来无一物,何处惹尘埃. |
|
2008-6-25 20:49 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
大家说了半天
都没觉得是在围着一个很不典型的例子在讨论问题吗?
难道没人知道set /a是缺省具有延迟效果的吗?
楼主的那个例子完全用不着变量延迟或者“中间结果”云云
@echo off
for /f %%a in ('dir/b') do set /a file_num+=1
echo 目录%cd%下有%file_num%个文件(夹)。
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%.
|
|
2008-6-25 21:09 |
|
|
metoo
初级用户
 
积分 195
发帖 93
注册 2006-10-28
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
- - 菜鸟而已
你的办法是想通过“变变量”的方式来获得各个步骤的,是一次性的生成n个不同的变量,这些变量需要在for命令完全执行完以后才能使用,这就是我说的取巧,而延迟变量是在for命令执行过程中就可以使用。。
@echo off
for /f "tokens=*" %%i in ('dir /b') do (
set /a n+=1 >nul
call set _%n%=%%i >nul
echo %n%
echo %_%
echo %_1%
)
echo\
echo\
echo\
::换行
::到这儿整个循环结束 %n% %_% 这两个个变量生效
echo %_%
echo %n%
echo %_1%
::验证到%_1%变量不存在
echo\
echo\
echo\
::换行
::上面这3句是来验证下循环中的变量是不是能使用(我自己加出来的)
call echo 目录下有%n%个文件(夹)
pause
Last edited by metoo on 2008-6-25 at 09:14 PM ]
- 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
echo %n%
echo %_%
echo %_1%
)
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 ]
|
|
2008-6-25 21:12 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
metoo老兄9楼的言论有欠思量啊
在没有变量延迟的dos时代
也是可以“连续赋值/中间环节直接获取”的
这只需要对被call的:num_add一节做些小小的改动就可以了
而使用for+call代替for+延迟的方案
通常都会把所有的循环体代码移植到被call的子程序中
而在子程序内部是可以想怎么用就怎么用
没有什么限制的
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.
|
|
2008-6-25 21:15 |
|