如何在/F的循环内设置变量 [英] How to set a variable inside a loop for /F
问题描述
我做了这个代码
dir /B /S %RepToRead% > %FileName%
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo %z%
echo %%a
)
echo %%a
工作正常,但 echo %z%
返回echo disabled".
echo %%a
is working fine but echo %z%
returns "echo disabled".
我需要设置一个 %z% 因为我想像 %z:~7%
I need to set a %z% because I want to split the variable like %z:~7%
有什么想法吗?
推荐答案
有两种方法可以在 for 循环和括号范围内设置和使用变量.
There are two methods to setting and using variables within for loops and parentheses scope.
setlocal enabledelayedexpansion
请参阅setlocal/?
以获取帮助.这仅适用于 XP/2000 或更新版本的 Windows.然后在循环内使用!variable!
而不是%variable%
...
setlocal enabledelayedexpansion
seesetlocal /?
for help. This only works on XP/2000 or newer versions of Windows. then use!variable!
instead of%variable%
inside the loop...
使用批量跳转标签:Label
创建一个批量函数.
Create a batch function using batch goto labels :Label
.
示例:
for /F "tokens=*" %%a in ('type %FileName%') do call :Foo %%a
goto End
:Foo
set z=%1
echo %z%
echo %1
goto :eof
:End
批处理功能是非常有用的机制.
Batch functions are very useful mechanism.
这篇关于如何在/F的循环内设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!