7zip Helper Batch File

7zip is an open source archive utility. 7zip has a nice GUI to create basic archive but you need to switch to command line if you want to include/exclude folders. It is boring to type same command repetitively if you have to take zipped backup periodically.

This tiny little batch file (backup.bat) creates password protected 7z archive of the specified folders in the folder where it resides. You don’t type your password in the batch file so you don’t give away the password. You can also specify names of the folder you want to exclude from the archive. As an example, by default, backup.bat archives folders named – Source & doc, and excludes bin,obj, and temp/test folders (see customization section below to know how to change).

Usage

Copy backup.bat file in the folder which has the subfolders you want to archive. Navigate to the folder in explorer and open command prompt in the same folder (shift+right click > Open command prompt here…) . Type backup.bat at the command prompt and follow the instructions. Pay attention to instructions which appear on your screen.

Requirements

7zip (http://www.7-zip.org/)
Powershell (Windows7 up, all versions have powershell by default)
7zip executable path in global path variable
Screenshot (19)

Customize

Following is the default 7zip command in the batch file –

7z.exe a -t7z yyyymmddhhmm_filename.7z Docs source -mhe=on -p%password% -xr!bin -xr!obj -x!temp/test

“Docs Source” specify the two folders – docs and source, to archive. You can add/change folders to include here. “-xr!bin -xr!obj -x!temp/test” specify 3 folders to exclude from archive – bin, obj, and temp/test. “r” switch in obj and bin denotes that bin and obj folder could be anywhere in the folder structure.

Visual Studio: Auto-increment Build Number – Part 2/3

Visual Studio: Auto-increment Build Number – Part 2/3

Part 1

Concept || Real

I am using the setup, we are discussing here, for last 8000+ builds I compiled on my dev box with Visual Studio. I can tell you that this is a robust code and pretty smoothly working setup.

What & How?

We will leverage powerful post-build event + PowerShell script (full script is discussed below) to have Visual Studio build process do what we want. We will do following:

  • Write aPowerShell script which does following tasks:
      Pick just-compiled dll from calling project’s folder
      Read File Version field
      Increment Build number field in the version by 1
      Write the new Version number with incremented Build number to AssemblyInfo.cs of the calling project
  • Write a wrapper batch file to call aforesaid PowerShell script
  • Configure project to call the batch file with appropriate params
  • Let Visual Studio know when to run the post-build event

After every build we will have our read-only BuildLog.txt file in project folder updated, like so:

BuildLog

Note: The highlighted header is manually inserted, script does not write the header automatically. If you want header in your BuildLog.txt, edit it manually once and insert header at the top.

So, the conceptual diagram of setup looks like this:

Setup

The Script

I wrote this PowerShell script using NotePad++. You will find script very similar to c# because PowerShell utilizes a lot of .NET.  If you wish to edit the script to extend its functionality, you can use any editor of your choice, e.g. PowerShell GUI Editor, which provides excellent debug support.
We will call our PowerShell script file UpdateBuildLog.ps1. Here is the outer structure of our script:

ScriptStructure

  1. File Header
  2. We will wrap our script in try catch block so as to ensure we know the reason of the script failure in our build output windows. We will see how.
  3. Parameter:projectPath – Path of the project, defaulted to c:\temp\
  4. Parameter:targetDLLFullPath – Where project’s dll is located, defaulted to c:\temp\
  5. Parameter:extraAttribute – Any extra text attribute for build log entry, defaulted to XXX
  6. Parameter:buildConfig – Build configuration, Debug|Release, defaulted to Debug

Let’s have a look at the internals of the script:

ScriptWorking

  1. Prepare AssemblyInfo.cs path from supplied project location. Write the prepared information to Project’s build output window.
  2. Read just created dll file from the supplied path and file name, and extract version and build number. Write version and build number to build output window. Increment build number by 1 in the extracted version number.
  3. Prepare Build log entry, write the entry into build log text file, make the build log text file readonly again.
  4. Update the AssemblyInfo file with the new version number.
  5. Confirm proper version number is written to AssemblyInfo file and write to build output window.
  6. If there is any issue while updating new version number, write to build output window.

The Batch

We will call our batch file PostBuild.bat.  We will setup our project’s post build event in Visual Studio to call PostBuild.bat with appropriate parameters. Following are important parts of the batch file:

BatchFile

  1. File Header
  2. Prepare parameters received from Visual Studio build events
  3. Call procedures on the basis of supplied switches
  4. Customize the batch file and write your code (by default, this section does nothing)
  5. Customize the batch file and write your code (by default, this section does nothing)
  6. Procedure to call PowerShell script to update build log with incremental build number.

We now have a decent idea of the nuts and bolts, and we  are clear about the structure of the setup, script and the batch file.

Code download and customization in – Part 3

Hope you are still with me.