Skip to content

Instantly share code, notes, and snippets.

@glombard
Last active September 30, 2019 09:50
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save glombard/11037630 to your computer and use it in GitHub Desktop.
Save glombard/11037630 to your computer and use it in GitHub Desktop.
TFS checkout files from a batch file
@echo off
setlocal EnableExtensions
set tfuser=glombard
set tflogin=/login:DOMAIN\%tfuser%,password
set tfworkspace=TFSWorkspace
set tfdir=C:\temp\MyWorkspace
set tfsource=$/PROJ/Source
set tfurl=http://127.0.0.1:8080/tfs/TEAM/
if "%1" == "clean" goto removeWorkspace
if exist %tfdir% goto dirOk
md %tfdir%
cd %tfdir%
:dirOk
tf > nul
if errorlevel 1 goto noTf
tf workspaces /owner:%tfuser% %tflogin% /computer:%COMPUTERNAME% | findstr %tfworkspace% > nul
if errorlevel 1 goto createWorkspace
echo Workspace '%tfworkspace%' already exists...
goto workspaceOk
:createWorkspace
tf workspace /new %tfworkspace%;%tfuser% %tflogin% /collection:%tfurl% /noprompt
if errorlevel 1 goto newWorkspaceFailed
echo Workspace '%tfworkspace%' created OK...
tf workfold /map %tfsource% %tfdir% /workspace:%tfworkspace% %tflogin%
if errorlevel 1 goto folderMapFailed
:workspaceOk
tf get %tfsource%/PROJ/src/FileToModify.txt %tflogin%
if errorlevel 1 goto getFailed
tf checkout %tfsource%/PROJ/src/FileToModify.txt %tflogin% /lock:checkin
if errorlevel 1 goto checkoutFailed
REM Now update FileToModify.txt ...
tf checkin %tfsource%/PROJ/PROJ/java /recursive /noprompt /comment:"Updated by TFS Build" %tflogin%
if errorlevel 1 goto checkinFailed
:done
echo SUCCESS: TFS has shown us mercy.
goto :eof
:checkinFailed
echo Check-in failed :-(
goto :eof
:getFailed
echo Failed to do Get Latest :-(
goto :eof
:checkoutFailed
echo Checkout failed :-(
goto :eof
:folderMapFailed
echo Failed to map %tfsource% -> %tfdir% :-(
goto :eof
:newWorkspaceFailed
echo Couldn't create workspace '%tfworkspace%' :-(
goto :eof
:noTf
echo TF.exe command-line utility not found!
goto :eof
:removeWorkspace
echo Removing the workspace '%tfworkspace%' completely!
tf undo /workspace:%tfworkspace% %tfsource% /recursive %tflogin%
tf workspace /delete %tfworkspace% %tflogin% /noprompt
cd \
rd %tfdir% /s /q
goto :eof
@echo off
setlocal EnableExtensions
REM -- edit these settings --
set tfuser=glombard
set tfdomain=MyDomain
set tfpassword=mypass
set tfcol=http://127.0.0.1:8080/tfs/MyCollection
set tfsource=$/PROJ
set tfgetlatest=/Source/Test.cs
set tfworkspace=TFSWorkspace
set tflogin=/login:"%tfdomain%\%tfuser%,%tfpassword%"
set tfdir=C:\temp\%tfworkspace%
if "%1" == "clean" goto removeWorkspace
if exist %tfdir% goto dirOk
md %tfdir%
cd %tfdir%
:dirOk
tf >nul 2>&1
if not errorlevel 1 goto tfOk
call "%ProgramFiles(x86)%\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat"
tf >nul 2>&1
if errorlevel 1 goto noTf
:tfOk
echo Checking workspaces...
tf workspaces /owner:%tfuser% %tflogin% /computer:%COMPUTERNAME% | findstr %tfworkspace% > nul
if errorlevel 1 goto createWorkspace
echo Workspace '%tfworkspace%' already exists...
goto workspaceOk
:createWorkspace
echo Creating workspace '%tfworkspace%' ...
tf workspace /new %tfworkspace%;%tfuser% %tflogin% /collection:%tfcol% /noprompt
if errorlevel 1 goto newWorkspaceFailed
echo Workspace '%tfworkspace%' created OK...
tf workfold /map %tfsource% %tfdir% /workspace:%tfworkspace% %tflogin%
if errorlevel 1 goto folderMapFailed
:workspaceOk
tf get %tfsource%%tfgetlatest% %tflogin%
if errorlevel 1 goto getFailed
:done
echo SUCCESS!
goto :eof
:getFailed
echo Failed to do Get Latest :-(
goto :eof
:folderMapFailed
echo Failed to map %tfsource% -> %tfdir% :-(
goto :eof
:newWorkspaceFailed
echo Couldn't create workspace '%tfworkspace%' :-(
goto :eof
:noTf
echo TF.exe command-line utility not found!
goto :eof
:removeWorkspace
echo Removing the workspace '%tfworkspace%' completely!
tf undo /workspace:%tfworkspace% %tfsource% /recursive %tflogin%
tf workspace /delete %tfworkspace% %tflogin% /noprompt
cd \
rd %tfdir% /s /q
goto :eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment