防止扩展:在batch filefor循环

尝试构build一个用于查看TFS变更集的cmdline工具。 目前我有这个:

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”,但是如果我删除/格式化,我会得到/版本中发生的相同的事情。

其次,如果我只是用空格来replace:假设我稍后再处理,我会得到这个错误

 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 

尝试:

 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命令,请改为指定%%variables
%variables。 variables名称区分大小写,所以%i是不同的
从%I。