How do I increment a value in a textfile using the regular Windows command-line?

戏子无情 提交于 2019-12-05 11:25:15

You can try a plain old batchfile.

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

assuming the count.bat and counter.txt are located in the same directory.

It would be an new shell (but I think it is worth it), but from PowerShell it would be

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

I'd suggest just appending the current datetime of the build to a log file.

date >> builddates.txt

That way you get a build count via the # of lines, and you may also get some interesting statistics if you can be bothered analysing the dates and times later on.

The extra size & time to count the number of lines in the file will be insignificant unless you are doing seriously fast project iterations!

If you don't mind running a Microscoft Windows Based Script then this jscript will work OK. just save it as a .js file and run it from dos with "wscript c:/script.js".

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!