how to use batch file variable outside inner for loop

ε祈祈猫儿з 提交于 2019-12-31 05:37:24

问题


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
)

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


回答1:


Again, 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!
)

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



来源:https://stackoverflow.com/questions/25495767/how-to-use-batch-file-variable-outside-inner-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!