65.9K
CodeProject 正在变化。 阅读更多。
Home

TFS 已检出文件批处理脚本

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2投票s)

2017年3月5日

CPOL

1分钟阅读

viewsIcon

9033

downloadIcon

67

一个批处理脚本,用于从 TFS 获取已检出和修改的文件,并将它们的文件路径保存到输出文件。

引言

有时您需要一个当前从 TFS 检出的所有文件的列表。Visual Studio 提供了按状态搜索窗口(文件 -> 源控制 -> 查找),但是如果您只需要一个包含您已检出文件的名称的文本文件怎么办?这时 TF.exe 就派上用场了。这个可执行文件的目的是为所有您已经使用 Visual Studio 和源控制资源管理器执行的 TFS 操作提供命令行功能。

在大多数情况下,TFS 目录映射到一个文件夹,因此您获得一对一的映射。这是此批处理脚本的前提条件。为了本文的目的,我将使用 TFS 目录 $/Projects/UserName/MyApp,它映射到本地文件夹 C:\Users\UserName\Documents\Visual Studio\Projects\MyApp。显然,您需要将这些设置更改为您自己的映射。脚本使用的 TF.exe 版本是 VS 2012 版本,您可以根据需要进行更改。

要检索更改的文件列表,脚本执行 Status 命令。对于每一行,它尝试确定该行是否包含在脚本开始时定义的本地文件夹。如果是,则提取完整的文件路径和相对文件路径。目前,脚本将相对路径输出到输出文件。用于打印完整路径的代码行被注释掉了,因此如果您需要它而不是其他路径,只需切换这些代码行的注释即可。如果您只需要将文件打印到命令行,可以删除重定向到 outputFile 的内容。

@echo off
setlocal EnableDelayedExpansion

set "folder=C:\Users\UserName\Documents\Visual Studio\Projects\MyApp\"
set "itemspec=$/Projects/UserName/MyApp/"
set "workspace=WorkspaceName"
set "tf=C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\TF.exe"
set "outputFile=outputFile.txt"

type nul>%outputFile%

call :strlen "%folder%", folderLength
if %folderLength% equ 0 goto :eof

set cmd="%tf%" stat %itemspec% /workspace:%workspace% /format:brief /recursive
for /f "delims=" %%G in ('%cmd%') do (
    set "line=%%G"
    call :strlen "!line!", lineLength
    if !lineLength! gtr 0 call :printFile %line%, !lineLength!, %folder%, %folderLength%, %outputFile%
)

goto :eof

::printFile(line, lineLength, folder, folderLength, outputFile)
:printFile
setlocal
set /a "lineLength -= folderLength"
if %lineLength% lss 0 endlocal & goto :eof
for /l %%i in (0,1,%lineLength%) do (
    call set "sub=%%line:~%%i,%folderLength%%%"
    if %folder% == !sub! (
        call set "fullPath=%%line:~%%i%%"
        set /a "index=%%i + folderLength"
        call set "relPath=%%line:~!index!%%"
        echo !relPath!>>%outputFile%
        ::echo !fullPath!>>%outputFile%
        endlocal
        goto :eof
    )
)
endlocal
goto :eof

::strlen(string, out length)
:strlen
setlocal
set "length=0"
set "#=%~1"
:strlenLoop
if defined # (set "#=%#:~1%" & set /a "length += 1" & goto strlenLoop)
endlocal & set "%~2=%length%"
goto :eof
© . All rights reserved.