Getting Rid of Very Long Directory Paths during msbuild process

My team recently encountered a problem during our build process where a left over intermediate file was not being properly deleted.  Even worse, this was not reported so during the compilation very strange errors appeared regarding constructors and methods that weren't in version control.

The issue was eventually tracked down as an issue with some file paths exceeding the 248 character limit.  Due to the excessively long file path, the delete failed.  <insert gasface here /> Why this is an issue in 2013, well that will be a topic for another time.  Instead, here is how to workaround the challenge.

  1. Obtain a batch script that will ruthlessly remove a directory
  2. Review this blog post about the inner workings of the exec msbuild task.  The short version is that a temporary script gets created, so if you want a return code the script should have a certain syntax at the end.
  3. Add to your msbuild proj file to have a BeforeBuild task that calls batch script from step 1 (modified as necessary from step 2).
  4. Configure the build tasks such that BeforeBuild is called prior to Build.