如何设置一个变量FOR / F在循环中 [英] How to set a variable inside a loop for /F
问题描述
我做了这个code
dir /B /S %RepToRead% > %FileName%
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo %z%
echo %%a
)
呼应%% A
工作正常,但回声%Z%
返回回声已禁用。
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%内循环...
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...
使用批处理转到标签创建一个批处理功能:标签
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.
这篇关于如何设置一个变量FOR / F在循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!