Build count of VC++ project for version string
For ages now I wanted to write me something nice and neat to get build count (how often the project was built) to append it to my version string. And obviously I wanted it to automatically update.
However I never really got around to it or cared that much that I actually wrote it. But today is the day!
And here is how it works:
@echo off SETLOCAL set TARGET_PATH=%~dp0 set FILENAME="%TARGET_PATH%build.h" set /p BUILD= < %FILENAME% set BUILD=%BUILD:~22% if /I "%BUILD%" == "" set BUILD=0 set /a BUILD=%BUILD%+1 echo #define VERSION_BUILD %BUILD% > %FILENAME% echo #define VERSION_DATE L"%DATE%" >> %FILENAME% echo #define VERSION_TIME L"%TIME%" >> %FILENAME% ENDLOCAL
In case it’s not obvious what this batch file does. First we construct our target filename so that it is still valid even if VS calls the batch from another directory. Then we read in the first line of our build.h (the VERSION_BUILD #define), then we cut off the part before the build number, add 1 and write it back. I also add 2 #defines for date and time, cause, why not? I made date and time already wide strings but left the build count an integer. This makes it slightly easier to parse for the batch file ;)
So just throw the two files in a folder in your project and run the batch once so the build.h is created. Then open up your project properties and add the batch as a new Pre-Build Event. It makes most sense just to do it for Release builds. Now whenever you wanna reference the build count, just #include the build.h. Done.
I guess it would be desirable to solve this whole thing without macros. But at this time I lack the energy to make it more complex. Maybe in a few years…
