2008-09-02 74 views
5

我想爲我的一個項目保留一個「編譯計數器」。我認爲一個快速和骯髒的方法是保持一個文本文件中包含一個普通數字,然後每次編譯時只需調用一個小腳本來增加它。如何使用常規的Windows命令行增加文本文件中的值?

我該如何使用常規的Windows命令行來做到這一點?

我真的不想安裝一些額外的外殼來做到這一點,但如果你有任何其他超級簡單的建議,可以實現這一點,他們也很自然地讚賞。

回答

11

您可以嘗試一個普通的舊批處理文件。

@echo off 
for /f " delims==" %%i in (counter.txt) do set /A temp_counter= %%i+1 
echo %temp_counter% > counter.txt 

假設count.bat和counter.txt位於相同的目錄中。

2

這將是一個新的shell(但我認爲這是值得的),但是從PowerShell的將是

[int](get-content counter.txt) + 1 | out-file counter.txt 
1

如果你不介意運行Microscoft基於Windows的腳本那麼這將JScript的工作好。只需將其保存爲.js文件並使用「wscript c:/script.js」從dos運行即可。

var fso, f, fileCount; 
var ForReading = 1, ForWriting = 2; 
var filename = "c:\\testfile.txt"; 
fso = new ActiveXObject("Scripting.FileSystemObject"); 

//create file if its not found 
if (! fso.FileExists(filename)) 
{ 
    f = fso.OpenTextFile(filename, ForWriting, true); 
    f.Write("0"); 
    f.Close(); 
} 

f = fso.OpenTextFile(filename, ForReading); 
fileCount = parseInt(f.ReadAll()); 

//make sure the input is a whole number 
if (isNaN(fileCount)) 
{ 
    fileCount = 0; 
} 

fileCount = fileCount + 1; 

f = fso.OpenTextFile(filename, ForWriting, true); 
f.Write(fileCount); 
f.Close(); 
2

我建議只需將構建的當前日期時間附加到日誌文件。

date >> builddates.txt 

這樣,你得到通過線路的#構建計數,你也可能得到一些有趣的統計數據,如果你可以打擾分析以後的日期和時間。

額外的大小&計算文件中行數的時間將是微不足道的,除非您正在進行嚴重的快速項目迭代!