我有以下代码,但它似乎没有工作。有人能指出我正确的方向吗?
setlocal
set BuildRoot=d:\
set OutDir=%Tmp%
call :GenerateBaseLine "Build"
goto :eof
:GenerateBaseLine
call dir /s /b /A-D %%1Root% > %OutDir%\%1Baseline.txt
echo "wrote dir /s /b /A-D" %%1Root% %OutDir%\%1Baseline.txt
goto :eof
我得到的输出是:
>setlocal
>set BuildRoot=d:\
>set OutDir=C:\Users\erickul\AppData\Local\Temp
>call :GenerateBaseLine "Build"
>call dir /s /b /A-D %1RootOutDir1Baseline.txt
File Not Found
>echo "wrote dir /s /b /A-D" %1RootOutDir1Baseline.txt
"wrote dir /s /b /A-D" %1RootOutDir1Baseline.txt
>goto :eof
>goto :eof
这段代码有什么问题?我期待
%%1Root%
评估为D:\
答案 0 :(得分:0)
您可以使用setLocal enableDelayedExpansion
按预期扩展变量。请尝试以下方法:
setLocal enableDelayedExpansion
set BuildRoot=d:\
set OutDir=%Tmp%
call :GenerateBaseLine "Build"
goto :eof
:GenerateBaseLine
call dir /s /b /A-D !%1Root! > %OutDir%\%1Baseline.txt
echo "wrote dir /s /b /A-D" !%1Root! %OutDir%\%1Baseline.txt
goto :eof
问题当然在于行call dir /s /b /A-D %%1Root% > %OutDir%\%1Baseline.txt
- 更具体地说,从%%1Root%
开始。您显然正在尝试展开%1
,然后展开%BuildRoot%
,但CMD将首先用%%
替换%
,然后尝试展开% > %
(展开为null ),然后%\%
(也为空)。离开你%1RootOutDir1Baseline.txt
。
启用延迟扩展后,CMD将首先展开%1
,%OutDir%
和%1
- 然后展开!BuildRoot!
。