Naeem Sarfraz

Blogging about Enterprise Architecture, ALM, DevOps & happy times coding in .Net

Timestamp a File or Folder in Release Management 2013

TLDR: Download and use this powershell script in your deployments to append a timestamp to a file or folder.

Typically you’ll backup a database prior to upgrading it through Release Management or you’ll copy a website folder to a backups folder as part of your rollback strategy. Both tasks are easily achieved using the Backup SQL Database and Copy File or Folder tasks.

RM Web DB tasks - no unique name

You’ll most likely want to version your backup copies using a timestamp strategy. This would involve passing some sort of token which can be evaluated at runtime and your input value modified to accommodate this token and this is where we find RM wanting. For example you can’t use a token such as __BuildID__ in the DestinationFileFolder (Copy File or Folder task) like so D:\Sites\InvoiceApp\Backups\InvoiceWebapp__BuildID__. The input fields simply won’t recognise any token so our only option is to follow up the task with a new one that will rename the newly created file or folder.

PowerShell to the rescue

Here is a powershell script which can be can be used to rename either a file or a folder. By default it will append a timestamp in the format yyyyMMddhhmmss but you can override that to match your requirements.

To begin to use this in RM you will need to create a Tool like this:

RM RenameObject Tool

And then create a couple of Actions like this:

RM RenameObject Action File RM RenameObject Action Folder

And finally use it in your template like this:

RM Suffix file RM Suffix Folder

An approach that didn’t work

For completeness I’m including an approach that didn’t work me and I never got to the bottom of why it never worked.

From the command line I can rename a file or folder using xcopy whilst at the same time I can use %date% and %time% to create a timestamp. A typical command would look like this:

xcopy D:\Sites\AppA\Current D:\Sites\AppA\Backups\AppA_%date:~-4,4%%date:~-7,2%%date:~-10,2%%time:~-11,2%%time:~-8,2%%time:~-5,2%%time:~-2,2% /I /E

You’d think then if I used this command in the Run Command Line task that we have the desired behaviour well RM doesn’t like this command. An error occurred from the task which looked like it was trimming down the command. I gave up after a few hours and pursued the solution above.

blog comments powered by Disqus