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

时间:2014-08-25 23:20:47

标签: batch-file scripting

我不熟悉批处理脚本变量作用域。但由于缺乏批处理脚本经验,我遇到了范围问题。

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
)

那么我如何在我的内部循环

之外使用%%文件

1 个答案:

答案 0 :(得分:2)

再次,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-loop和!File!外部使用%File%