Improved AutoIncreasing build number and other macros

I really love using macros in VC++. They make things highly customizable, like I like them. Not how MS wants me to use it.

So, I took the liberty to add to Thomas Mahler's wonderful macro for increasing build number. I added support for increasing ALL the build numbers avaliable in the .rc file. I also added backing up the file, because I noticed in certain situations, it would occasionally change settings and lines other than the build numbers. So use with caution.

I added putting the current compile date/time to the 2nd line. I also include here, a couple macros for backing up the current active document, and restoring the .rc file.

First, the small macros-

Sub CurrentDateTime()
 'DESCRIPTION: Saves current date/time to the rc file, on line 2.

    Dim strMonth
    Dim strDay
    Dim strYear    
    Dim strHour
    Dim strMinute
    Dim strSecond

	Documents.Open (ActiveProject.Name + ".rc"), "Text"
	Windows(ActiveProject.Name + ".rc").Active = TRUE
	ActiveDocument.Selection.GoToLine 2
	ActiveDocument.Selection.SelectLine 
	ActiveDocument.Selection.Delete
	
	strMonth = CStr(DatePart("m", Now)) 
	strDay = CStr(DatePArt("d", Now))
	strYear = CStr(DatePart("yyyy", Now))
	strHour = CStr(DatePart("h", Now)) 
	strMinute = CStr(DatePArt("n", Now))
	strSecond = CStr(DatePart("s", Now))

	ActiveDocument.Selection.Text ="// Last compiled "+strMonth + "/" +strDay +"/" +strYear+" at "+ strHour+":"+strMinute+":"+strSecond+    + vbCrLf
	ActiveDocument.Selection.NewLine
	ActiveDocument.Save (ActiveProject.Name + ".rc")
	ActiveDocument.Close() 

End Sub

Sub saveBackUpRC()
'DESCRIPTION: Backs up rc file.

    Documents.Open (ActiveProject.Name + ".rc"), "Text"
    Windows(ActiveProject.Name + ".rc").Active = TRUE
    ActiveDocument.Save (ActiveProject.Name + ".rc"+"~")
    ActiveDocument.Close() 

End Sub


Sub restoreRcFile
'DESCRIPTION: Restores back up of rc file.

    Documents.Open (ActiveProject.Name + ".rc~"), "Text"
    Windows(ActiveProject.Name + ".rc~").Active = TRUE
    ActiveDocument.Save (ActiveProject.Name + ".rc")
    ActiveDocument.Close() 

End Sub


Sub saveActiveDocBackup()
'DESCRIPTION: Backs up current active document.

    Dim Wnd
    Wnd = ActiveDocument.FullName
    ActiveDocument.Save(ActiveDocument.fullname + "~")
    ActiveDocument.Close() 
    Documents.Open Wnd, "Text"

End Sub	

Now, the improved build increase macro

Sub IncResVersion()
'DESCRIPTION: Build active project and increase version counters in rc script.
' Map it to F7 for ease of use. (c)'98 Th. Mahler and parts by LJP.
	

    Dim strMonth
    Dim strDay
    Dim strYear    
    Dim strHour
    Dim strMinute
    Dim strSecond

	Dim Wnd
		' open the project's resource script:
	Wnd = ActiveProject.Name + ".rc"
	Documents.Open (ActiveProject.Name + ".rc"), "Text"
	Windows(ActiveProject.Name + ".rc").Active = True

	  ' SAVE BACK-UP OF FILE >>>>
	ActiveDocument.Save (ActiveProject.Name + ".rc"+"~")

	ActiveDocument.Close() 
	Documents.Open Wnd, "Text"
  
	'Go to 2nd line in rc file, delete it, and get current time/date
	ActiveDocument.Selection.GoToLine 2
	ActiveDocument.Selection.SelectLine 
	ActiveDocument.Selection.Delete

	strMonth = CStr(DatePart("m", Now)) 
	strDay = CStr(DatePArt("d", Now))
	strYear = CStr(DatePart("yyyy", Now))
	strHour = CStr(DatePart("h", Now)) 
	strMinute = CStr(DatePArt("n", Now))
	strSecond = CStr(DatePart("s", Now))

	'write current date/time and add newline - formatted for USA
	ActiveDocument.Selection.Text ="// Last compiled "+strMonth+ "/"+strDay +"/"+strYear+" at "+strHour+":"+strMinute+":"+strSecond    + vbCrLf
	ActiveDocument.Selection.NewLine

		' find line with FileVersion Information:
	ActiveDocument.Selection.FindText "FILEVERSION", dsMatchForward + dsMatchFromStart + dsMatchCase  

	' move to eol and then to end of build number:
	ActiveDocument.Selection.EndOfLine

	' mark Build Number and store in strVersion
	ActiveDocument.Selection.WordLeft 1  'dsExtend does not work in my VisualStudio???
	Dim strVersion3
	strVersion3 = ActiveDocument.Selection.Text 

	' Increase Version and write back to file: 
	ActiveDocument.Selection.ReplaceText strVersion3 , strVersion3+1 

	ActiveDocument.Selection.FindText "PRODUCTVERSION", dsMatchForward + dsMatchFromStart + dsMatchCase  

	' move to eol and then to end of build number:
	ActiveDocument.Selection.EndOfLine

	' mark Build Number and store in strVersion
	ActiveDocument.Selection.WordLeft 1  'dsExtend does not work in my VisualStudio???
	Dim strVersion4
	strVersion4 = ActiveDocument.Selection.Text 

	' Increase Version and write back to file: 
	ActiveDocument.Selection.ReplaceText strVersion4 , strVersion4+1 
		

	ActiveDocument.Selection.FindText "VALUE ""FileVersion"",", dsMatchForward + dsMatchFromStart + dsMatchCase  

	' move to eol and then to end of build number:
	ActiveDocument.Selection.EndOfLine
	ActiveDocument.Selection.CharLeft dsMove, 3 

	' mark Build Number and store in strVersion
	ActiveDocument.Selection.WordLeft 1  'dsExtend does not work in my VisualStudio???
	Dim strVersion
	strVersion = ActiveDocument.Selection.Text 

	' Increase Version and write back to file: 
	ActiveDocument.Selection.ReplaceText strVersion , strVersion+1 

	ActiveDocument.Selection.FindText "VALUE ""ProductVersion"",", dsMatchForward + dsMatchFromStart + dsMatchCase  

	' move to eol and then to end of build number:
	ActiveDocument.Selection.EndOfLine
	ActiveDocument.Selection.CharLeft dsMove, 3 

	' mark Build Number and store in strVersion
	ActiveDocument.Selection.WordLeft 1  'dsExtend does not work in my VisualStudio???
	Dim strVersion2
	strVersion2 = ActiveDocument.Selection.Text 

	' Increase Version and write back to file: 
	ActiveDocument.Selection.ReplaceText strVersion2 , strVersion2+1 

	ActiveDocument.Close() 

	'build active project
	ExecuteCommand "BuildToggleBuild"
	Documents.SaveAll True  

End Sub 




Comments

  • How can I automatically reload a macro when Opening a project?

    Posted by Legacy on 11/17/1999 12:00am

    Originally posted by: Karl Brown

    I figured out a way to automatically check-out
    
    the .rc file from SourceSafe, modify the .rc file version
    and product version (by combining code and suggestions
    from LPJ, T. Mahler and Babu, Thank You All Very Much!),
    and check it back in. All in response to any Build event.

    However, I still have an annoying problem:
    Whenever I Open a workspace/project from SourceSafe it
    seems to confuse the IDE about the whereabouts of my
    macro (which I also included in the project) and I have
    to reload the macro thru the Tools|Customize|Macros and
    Add-ins tab to get proper operation. Does anyone know
    why this happens or at least a way around it?

    Thanks in advance for any advice,
    KB

    Reply
  • Build No for dependent projects

    Posted by Legacy on 09/22/1999 12:00am

    Originally posted by: dashali

    How this auto build version numbering can be done for a project which has sub projects as dependencies?

    Reply
  • Corrupts .rc file

    Posted by Legacy on 09/15/1999 12:00am

    Originally posted by: Peter Cullen

    After 6 or 7 builds, I get an error saying that the code_page is invalid. Examining the .rc file in text mode, I can see that the code_page was changed and the dimensions of all of my dialogs were changed! Is there a fix for this?

    Reply
  • Do you have to be in DevStudio to execute macros!?

    Posted by Legacy on 03/24/1999 12:00am

    Originally posted by: Gary MacDougall

    I'd love to be able to do this during my command line build scripts, is it possible to run macros outside of DevStudio using say NMAKE?

    Just wondering...

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Open source and cloud computing have long promised cost efficiencies, yet many organizations have shied away from these technologies due to security and reliability concerns. Now, open source has proven itself stable, and the cloud has become as secure—or even more secure—than on-premises implementations. Read this white paper to learn how you can get the accessibility of open source and the flexibility and affordability of cloud computing combined in a compelling storage option for companies …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds