sebastian87
sebastian87

Reputation: 484

Determine local path of a TFS Workspace via tf.exe

I am using a batch script for getting the latest version of specific projects. This script only runs tf.exe and gets the latest version of some Binaries. Everything works fine, but I would like to change the attrib of the downloaded files to be writeable (by deafult these files are read-only). For that I want to determine the local path of the files and use the attrib-command from batch.

tf.exe workfold [Workspace] shows me the local path in some kind of listing but it would be easier, if it only shows me what I want so I can use the prompt. Until now the it looks like this:

tf.exe workfold [Workspace]

=======================================

Arbeitsbereich: XYZ-xxxxxx (Username)

Auflistung: TFS-URL

[Workspace]:  C:\xxx\TFS\xxx

Is it possible to determine only the local path mapping of a TFS Workspace so that I can use the prompt for the attrib-command without parsing?

Upvotes: 5

Views: 4786

Answers (3)

Johny Skovdal
Johny Skovdal

Reputation: 2104

You can also do it without any string manipulation, with calls to TF.exe. I have wrapped that in PowerShell scripts, so you get the following:

function Add-TfsTypes
{
  # NOTE: Not all of the below are needed, but these are all the assemblies we load at the moment. Please note that especially NewtonSoft dll MUST be loaded first!
  $PathToAssemblies = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"
  Add-Type -Path "$PathToAssemblies\NewtonSoft.Json.dll"
  Add-Type -Path "$PathToAssemblies\System.Net.http.formatting.dll"
  Add-Type -Path "$PathToAssemblies\Microsoft.TeamFoundation.Client.dll"
  Add-Type -Path "$PathToAssemblies\Microsoft.TeamFoundation.Common.dll"
  Add-Type -Path "$PathToAssemblies\Microsoft.TeamFoundation.VersionControl.Client.dll"
  Add-Type -Path "$PathToAssemblies\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
}

function Get-TfsServerPathFromLocalPath {
  param(
    [parameter(Mandatory=$true)][string]$LocalPath,
    [switch]$LoadTfsTypes
  )

  if ($LoadTfsTypes) {
    Add-TfsTypes # Loads dlls
  }

  $workspaceInfo = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.GetLocalWorkspaceInfo($LocalPath)
  $server = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection $workspaceInfo.ServerUri
  $workspace = $workspaceInfo.GetWorkspace($server)

  return $workspace.GetServerItemForLocalItem($LocalPath)
}

The above method can then be called like this:

$serverFolderPath = Get-TfsServerPathFromLocalPath $folderPath -LoadTfsTypes
$anotherServerPath = Get-TfsServerPathFromLocalPath $anotherItemToTestPathOn

Upvotes: 1

Mak Sim
Mak Sim

Reputation: 2314

Real men do it in one line

powershell -command "& {tf workfold | Select-String -pattern:' $' -SimpleMatch | Select-Object Line | ForEach-Object {$startIndex = $_.Line.IndexOf(': ') + 2; $_.Line.subString($startIndex, $_.Line.length - $startIndex - 1)}}"

Current answer will only return one last path if there are many.

Upvotes: 1

Alexander Schmidt
Alexander Schmidt

Reputation: 5723

What about the following (crude!!!) concept?

function Get-TfsWorkfold([string]$TfsCollection, [string]$TfsWorkspace)
{
    $TfExePath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio 10.0\Common7\IDE\TF.exe"
    Write-Output "Getting workfold for '$TfsCollection'->'$TfsWorkspace'..."
    Push-Location $LocalPath
    & "$TfExePath" workfold /collection:$TfsCollection /workspace:$TfsWorkspace
}

function Handle-Path()
{
    param([Parameter(ValueFromPipeline=$true,Position=0)] [string] $line)
    $startIndex = $line.IndexOf(': ') + 2;
    $correctedLine = $line.subString($startIndex, $line.length - $startIndex - 1);
    Write-Output $correctedLine;
    Get-ChildItem $correctedLine
}

Get-TfsWorkfold "{serverAndcollection}"  "{workspace}" > c:\temp\test.txt
Select-String c:\temp\test.txt -pattern:': ' | Select-Object Line | Handle-Path

The last line in Handle-Path is the example which you can rewirte with whatever you want to. It is PowerShell but it should work as you want.

Replace {serverAndcollection} and {workspace}.

Upvotes: 1

Related Questions