How to: Label items in a changeset using tf label?

We can specify the versionspec to tf label either by supplying the /v or /version argument (for example, /v:C1256), or by appending it to the itemspec (for example, foo.cs;C1256). Here's an example that attaches the "goodbuild" label to version 1256 of $/proj/foo.cs:

tf label goodbuild /v:1256 $/proj/foo.cs

What about labeling all items in a certain changeset? We need to get a list of these items first:

tf changeset 1256

But this will invoke the changeset dialog, so we have to use the /noprompt  or /i to get the output in the command prompt window:

tf changeset 1256 /i

The output includes extra info like changeset number, owner, date, comment, and check-in notes. To extract the lines of the Items section, which contains the itemspecs, pipe the output to the find command:

tf changeset 1256 /i | find "$/"

This should work just fine as long as "$/" doesn't show up else where, in a comment for instance. Apply more filters if necessary! The output should look like the following:

    add        $/proj/foo.cs
    edit       $/proj/fold/bar.cs
    ...

We still got the Change column to get rid of, this can be accomplished using the parsing keywords of the for command that we will use for iteration:

for /f "usebackq tokens=2 delims=$" %i in (`tf changeset 1256 /i ^| find "$/"`) do tf label goodbuild /v:1256 $%i

We used the for command to iterate, the /f option specifies file parsing, and the "usebackq" parsing keyword indicates that the input file is the output of the command in the back quoted string, which in this case is:

`tf changeset 1256 /i ^| find "$/"`

We added the caret "^" to escape piping "|", otherwise, we will get the following error message:

| was unexpected at this time.

The parsing keywords do a great job to parse each line; "token=2 delims=$" passes the 2nd token from each line to the for body, with tokens delimited by $. That's why we had to prepend the $ to the itemspec in the for body as we lost it while parsing:

tf label goodbuild /v:1256 $%i

We may use any of the ways we saw before to specify the versionspec, such as:

tf label goodbuild $%i;C1256

So, the command would look like the following:

for /f "usebackq tokens=2 delims=$" %i in (`tf changeset 1256 /i ^| find "$/"`) do tf label goodbuild $%i;C1256