【发布时间】:2017-01-19 12:25:30
【问题描述】:
我想遍历当前目录中的所有 .h 文件,并为相应的 .cpp 文件调用批处理函数。实际上我已经让它工作了,但是当我尝试使用局部变量时,我得到了一些奇怪的输出。
SETLOCAL
for /R %%f in (*.h) do (
SET header=%f%
ECHO header=%header%
SET source=%%~df%%~pf%%~nf.cpp
ECHO source=%source%
)
我得到这个输出:
SETLOCAL
C:\WorkingDirectory>(
SET header=
ECHO header=
SET source=C:\WorkingDirectory\SomeFile.cpp
ECHO source=
)
header=
source=
为什么%%~df%%~pf%%~nf.cpp 正确展开,但ECHO %source% 什么也没打印?我怎样才能正确SET header=%f%?
【问题讨论】:
-
%f%实际上应该读为%%f,甚至更好,%%~f以删除潜在的周围引号...对于阅读header,您需要使用!header!而不是%header%;为了使这个延迟扩展生效,请将参数EnableDelayedExpansion添加到setlocal命令...
标签: windows batch-file filenames