我不熟悉批处理脚本变量范围。但由于缺乏批处理脚本经验,我遇到了范围界定问题。
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 循环之外使用 %%File
最佳答案
再次,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 循环内使用 !File!
并在 for 循环外使用 %File%
即可。
关于batch-file - 如何在内部for循环之外使用批处理文件变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25495767/