2014-02-28 40 views
0

为什么是它下面给出的输出我想到:Windows批处理文件:定义内从一个文件变量循环

@echo off 

echo foobar > tmp.txt 

SET /P a= < tmp.txt 
echo %a% 

FOR /F %%Q IN (tmp.txt) do set b=%%Q 
echo %b% 

foobar foobar

虽然下面的做没有:

@echo off 
FOR /L %%n in (1,1,1) DO (
    echo foobar > tmp.txt 

    SET /P a= < tmp.txt 
    echo %a% 

    FOR /F %%Q IN (tmp.txt) do set b=%%Q 
    echo %b% 
) 

ECHO is off. 
ECHO is off. 

一切我能找到给出了怎么办loop环境以外的东西方向。


回答指导大卫Ruhmann

@echo off 
setlocal ENABLEDELAYEDEXPANSION 

FOR /L %%n in (1,1,1) DO (
    echo foobar > tmp.txt 

    SET /P a= < tmp.txt 
    echo !a! 

    FOR /F %%Q IN (tmp.txt) do set b=%%Q 
    echo !b! 
) 
+2

您需要为变量使用**延迟扩展**。请参阅['setlocal/?'](http://ss64.com/nt/setlocal.html)和[EnableDelayedExpansion](http://ss64.com/nt/delayedexpansion.html)。或[搜索StackOverflow](http://stackoverflow.com/search?q=%5Bbatch-file%5D+delayed+expansion) –

+0

谢谢!我没有足够的声望来填写下面的答案,但是你给了我所需要的一切。 – ramzikai

回答

0

的“ECHO处于关闭状态”的响应表明,回声没“看到”任何命令行条目。

在failling例如,不使用第一FOR循环所产生的来自“N” %% n和,和你确定在(1,1,1)实际运行通过循环一次?

- 后编辑 -

所以“ECHO处于关闭状态”,足以证明它确实运行循环一次。所以我没有看到代码的特定缺陷。这使我锻炼我的谷歌福:

http://www.robvanderwoude.com/variableexpansion.php

希望有所帮助。

+0

我没有使用'%% n',因为我试图简化示例。在我的实际脚本中,我使用'%% n'来运行更新'tmp.txt'内容的脚本,然后我需要读入我的批处理文件。 – ramzikai

+0

另外,感谢您关于正确格式的提示。 :) – ramzikai

相关问题