@echo off
:: -----------------------------------------------------------------------------------
:: Demonstration of how to get user's local folder mapping of a TFS Code tree
:: -----------------------------------------------------------------------------------
CALL :GetLocalFolderMapping $/WorkSpaceName/FolderName
echo:
echo ------------------------------------------------------------------------------
echo Local folder path is:
echo:
echo "%LocalFolderPath%"
echo ------------------------------------------------------------------------------
:: -------------------------------------------------------------------------------------------------------------------
:: Finish up
:: -------------------------------------------------------------------------------------------------------------------
:VeryEnd
pause
exit 0
goto:eof
:: -----------------------------------------------------------------------------------
:: Subroutine to get local folder mapping of logged-in-user's synched TFS code tree.
:: -----------------------------------------------------------------------------------
:GetLocalFolderMapping
:: -----------------------------------------------------------------------------------
:: Usage:
:: CALL :GetLocalFolderMapping $/WorkspaceName
:: CALL :GetLocalFolderMapping $/WorkspaceName/FolderName
::
:: Then do something with the variable %LocalFolderPath%, such as:
:: Copy "%LocalFolderPath%\MainSolutionFile.sln" c:\temp\backup.sln
::
:: Details:
::
:: Queries Team Foundation Server with the TF.EXE command line utility that is shipped
:: with Visual Studio to ask what the local folder mapping is for a particular server
:: code tree workspace. For example, if you have a source code tree on the server of,
:: for example, $/SendBus/Development, and you need to programmatically determine what
:: the current user's local mapping of that folder is (for example, if you need to
:: perform a file operation on one of the files in the local folder and you need to
:: know its local hard disk location), then you can run this program to find the
:: local folder name that is mapped to that server-side TFS directory name.
::
:: Requires:
:: - User is logged in and has permissions on TFS.
:: - User has a fairly recent version of Visual Studio installed
:: - You need to know the server's name for the mapping.
:: - The user must have already mapped that thing.
:: -----------------------------------------------------------------------------------
:: Which workspace do we want to know the local folder mapping for?
:: User must already have a mapping for this in their TFS configuration.
:: This workspace is supplied as the %1 parameter to the call to this subroutine.
set WORKSPACE=%1
:: Save off the current directory folder, because we are going to change it and we want to
:: change it back after we're done.
PUSHD
:: Locate latest version of Visual Studio on user's hard disk.
:: Search for multiple possible versions of VS in user's program files folder.
:: This will use the highest-alphabetical version it finds in the Program Files folder
:: by simply looping through all of them and doing a SET command for each one, the last one
:: it finds is the last SET that sticks.
for /d %%a in ("%ProgramFiles(x86)%\Microsoft Visual Studio*") do set "LatestVS=%%~a"
:: Verify that it located at least one version of Visual Studio, quit if not.
IF "%LatestVS%"=="" CALL :ErrorMessage "Could not locate an installed instance of Visual Studio."
:: CD to that location.
echo Visual Studio found: %LatestVS%
cd /D "%LatestVS%"
:: CD to the common folder, then to the IDE folder beneath that.
cd common*
cd IDE
:: Make sure that there is a TF.EXE in this location, quit if not.
IF NOT EXIST TF.EXE CALL :ErrorMessage "Could not locate TF.EXE in the Common*\IDE folder under %LatestVS%."
:: Call TF.EXE with the parameters to locate what we are looking for.
:: Display the query to the screen so that if there is an error the user will see it.
echo Querying TFS for local working folder of %WORKSPACE%
TF.EXE WORKFOLD %WORKSPACE%
echo:
:: Do the query again, but this time pipe it through a command which will filter out the one variable we really want.
:: The output looks like this:
:: ===============================================================================
:: Workspace : SOMENAME (User Name)
:: Collection: https://my.tfsserver.com/tfs/SomeName
:: $/WorkSpaceName/FolderName: C:\Users\UserName\Source\Workspaces\WorkSpaceName\FolderName
::
:: so, skip to the 4th line and get the second 'token' on that line, which is the local path.
::
FOR /f "skip=3 tokens=2" %%a IN ('TF.EXE WORKFOLD %WORKSPACE%') DO set LocalFolderPath=%%a
:: Verify that we actually got a local folder mapping, error out if not.
IF "%LocalFolderPath%"=="" CALL :ErrorMessage "Could not locate a local folder mapping for %WORKSPACE%."
:: Before exiting the subroutine, set the current directory folder back to what it was before we started.
POPD
:: Environment variable %LocalFolderPath% now contains the user's local mapping of %WORKSPACE%
:: After returning from this subroutine, use the variable %LocalFolderPath% as you see fit
goto:eof
:: -------------------------------------------------------------------------------------------------------------------
:: Error Routine, exit program with a message.
::
:: Parameter: The error message.
:: -------------------------------------------------------------------------------------------------------------------
:ErrorMessage
echo:
echo:
echo ----------------------------------------------------------------------------
echo An error occurred:
echo %1
echo ----------------------------------------------------------------------------
echo:
pause
exit 1
goto:eof