Using relative paths to source for the Web Deploy tool (msdeploy.exe)

A colleague of mine recently wanted to write a script employing MSDeploy. Understandably he wanted to make the deployment scripts portable so that they could be used on different machines without forcing the deployment from and to the same directories on the different machines. Simple he thought: relative paths. I would have thought the same thing.

He expected to do something like the following (shortened for illustrative purposes):

msdeploy.exe -source:contentPath=".\SourceDir"

Simple until you discover that the tool requires either absolute paths or a full site/application path. This is clearly documented here. This documentation states that environment variables can be used and names %WINDIR% as an example.

The simplicity of the solution (also discovered by my colleague) was to delve back into DOS scripting days and use %CD%. This is the environment variable for the current directory. (Typing the quotes as well) try "%CD%" on the command line and see what you get. Just what you want for:

msdeploy.exe -source:contentPath="%CD%\SourceDir"