如何使用批处理文件变量外内循环

问题描述:

我不熟悉的批处理脚本变量范围。但是,我遇到了范围界定问题,由于我缺乏批处理脚本的经验。

I am not familiar with batch script variable scoping. But I ran into scoping problem due to my lack of batch script experience.

for /f %%i in ('dir /s /b "%FolderLocation%"') do (
set fullpathandfilename=%%i ::
For %%A in ("%fullpathandfilename%") do (
Set File=%%~nxA
echo %%File :: this would work and print out only filename
)
echo %File% ::this will not have filename I extracted above
)

因此​​,如何我可以使用%%文件超出了我内心的for循环

So how can i use %%File outside my inner for loop

再次 EnableDelayedExpansion

setlocal enabledelayedexpansion

for /f %%i in ('dir /s /b "%FolderLocation%"') do (
For %%A in ("%%~i") do (
Set File=%%~nxA
echo !File!
)
:: This shows how "%" doesn't work but "!" does
Echo ^%File^%: %File%   -   ^!File^!: !File!
)

和应该为你工作。只要记住使用!文件!里面一个for循环和%文件%之外。

And that should work for you. Just remember to use !File! inside a for-loop and %File% outside.