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

标签 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
)

那么我如何在内部 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/

相关文章:

linux - 正确使用 IO 重定向将用户输入附加到 Linux 脚本中的文件?

windows - 在 Batch 中显示变量的问题

windows - 运行时更改批处理文件

javascript - 在 Delphi 中使用 TWebBrowser 时如何抑制脚本错误警告?

linux - 无法在 bash Linux 中为整数赋值

linux - perl 脚本在命令行上运行但不在 crontab 中运行

regex - 混合 FOR 和 FINDSTR 时出现荒谬的插入符转义序列

windows - 如何使用批处理文件/脚本更改系统日期格式?

Windows 批处理多任务问题——代码清理

shell - shell脚本中的全局环境变量