Tuesday, May 7, 2013

Microsoft Tfs: Getting the files from a label and setting the right/real timestamps

I've been using Visual SourceSafe a lot earlier so when I started working with Team Foudation Server version control I took features from the VSS a little too much for granted.

Being able to get the right timestamp for files was one of them. In Visual SourceSafe you had a couple of options to choose to set as timestamp but with TFS there's is none. When you get the files, latest or from labels, the files timestamp is always current time.

This might work ok if you are doing your projects the Microsoft (only) way but I don't think most of us are. I had to find a way to do simple releases and I needed the right timestamps so the scripts would be able to know which files are updated and which are not.

There are some applications that try to correct this design flaw but with most of the systems I work with it's not possible just to download some application from the internet and start using it in a live production environment.

So for people like me the only solution is to write your own scripts or copy one from the internet.

This is simple Powershell script. You can name it what you like. You need to install PowerTools and specially select the Addin for Powershell in the installation application to be able to use this.

You have to also set Powershell execution policy( start from dos prompt):
Set-ExecutionPolicy RemoteSigned

Then to check that you have installed PowerTools correctly enter:
Add-PSSnapin Microsoft.TeamFoundation.Powershell
This script is used after you have done Get Label. (Normally you do 'Get Label' when want to do a release, not 'Get Latest')
It has to parameters, a path to directory in your working folder and the label that you used for 'Get'. It recursively gets the real timestamp for every file in working directory.


Add-PSSnapin Microsoft.TeamFoundation.Powershell

if(-not ( Test-Path $path )){
    Write-Error "$path is an invalid path."
    return $false

Get-TfsChildItem $path -recurse -version "L$label" |
        $_.ItemType -eq "File" } |
        $TFSFile = get-tfsitemproperty $_.ServerItem;
            $LocalFile = Get-Item ($TFSFile.LocalItem);
            $LocalFile.IsReadOnly = $false;
            Write-Output "$($_.CheckinDate) $($_.ChangeSetId) $($LocalFile.FullName)"
            $CheckinDate = $_.CheckinDate;
            $LocalFile.LastWriteTime = $CheckinDate;
            $LocalFile.IsReadOnly = $true;

It outputs every files Check-In-date(the real timestamp), ChangeSetId and file's full name. Remember that the check-in date and changesetid are not the latest one's but the one's that are in the label. There is not much code but I know it might take some time to figure it out if you hadn't done much Powershell scripting before.

No comments:

Post a Comment