Batch-File

防止擴展:在批處理文件中for循環

  • August 31, 2010

嘗試建構用於查看 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”,但是如果我刪除 /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

嘗試:

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 不同。

引用自:https://serverfault.com/questions/176164