如何使用批处理文件变量外内循环 [英] how to use batch file variable outside inner for loop

查看:167
本文介绍了如何使用批处理文件变量外内循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于如何使用批处理文件变量外内循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆