如何使用常规Windows命令行增加文本文件中的值?

时间:2020-03-05 18:46:47  来源:igfitidea点击:

我想为我的一个项目保留一个"编译计数器"。我想出了一种快速而又肮脏的方法来执行此操作,即在其中保留一个带有纯数字的文本文件,然后在每次编译时仅调用一个小脚本来对此进行递增。

我将如何使用常规Windows命令行执行此操作?

我真的不希望安装一些额外的shell来执行此操作,但是如果我们有其他任何超级简单的建议也可以做到这一点,那么他们自然也会受到赞赏。

解决方案

回答

这将是一个新的shell(但我认为这是值得的),但是从PowerShell中它将是

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

回答

如果我们不介意运行基于Windows的Microscoft脚本,则此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();

回答

我们可以尝试使用普通的旧批处理文件。

@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位于同一目录中。

回答

我建议仅将构建的当前日期时间添加到日志文件中。

date >> builddates.txt

这样,我们就可以通过的行获得构建计数,并且如果以后可以麻烦地分析日期和时间,则还可以获得一些有趣的统计信息。

除非我们认真进行快速的项目迭代,否则额外的大小和时间来计算文件中的行数将是微不足道的!