![防止批处理文件中 for 循环的扩展:](https://linux22.com/image/523023/%E9%98%B2%E6%AD%A2%E6%89%B9%E5%A4%84%E7%90%86%E6%96%87%E4%BB%B6%E4%B8%AD%20for%20%E5%BE%AA%E7%8E%AF%E7%9A%84%E6%89%A9%E5%B1%95%EF%BC%9A.png)
尝试构建一个用于查看 TFS 变更集的命令行工具。目前我有这个:
rem I know there's redundancy here, but don't care for now
set /A curr=%1
set /A prev=%curr%
set /A prev-=1
for /f "tokens=2" %g in ('tf changeset /noprompt %curr%') do tf diff /noprompt /format:unified /version:C%prev%~C%curr% %g
结果如下:
g:\>tfdiffchangeset.bat 2458
currunified was unexpected at this time.
我甚至不确定为什么:会变成“curr”,但如果我删除/format,/version 中也会发生同样的事情。
其次,如果我只是用空格替换 :'s 并假设我稍后会处理它,我就会收到此错误
g:\>tfdiffchangeset.bat 2458
The following usage of the path operator in batch-parameter
substitution is invalid: %~C%curr% %g
For valid formats type CALL /? or FOR /?
The syntax of the command is incorrect.
是时候编写 tfdiffchangeset.pl 了吗?
最终版本:
@ECHO off
set /A CURR=%1
rem Note - just using one changeset less doesn't necessarily work, because branches also use the same changeset numbers
set /A PREV=%CURR%-1
echo diffs for %CURR%
tf changeset /noprompt %CURR%
for /f "tokens=2" %%g in ('tf changeset /noprompt %CURR%') do tf diff /noprompt /format:unified /version:"C%PREV%~C%CURR%" %%g
答案1
尝试:
for /f "tokens=2" %%g in ('tf changeset /noprompt %curr%') do tf diff /noprompt /format:unified /version:C%prev%~C%curr% %%g
从HELP FOR
:
要在批处理程序中使用 FOR 命令,请指定 %%variable 而不是
%variable。变量名区分大小写,因此 %i
与 %I 不同。