Mass Rename TFS Source Control Files

Have you encountered a situation where you have to rename 1000’s of TFVC source control files?

We are working on code cleanup task. We have some 9000 SQL files. Out of those around 2000 have “.TAB” extensions like xxxx.TAB.  So, the task was to rename these “.TAB” files to “.SQL” which are in TFVC source control.

One way to do was manually renaming files which is tedious and not recommended approach.  I used a PowerShell script to update all “.TAB” to “.SQL” extensions. In my first attempt by using below script I could rename all files.

Get-ChildItem *.tab | Rename-Item -NewName {$_.Name.Replace(‘.TAB’,’.SQL’)}

To my horror, extensions to all files got changed but they didn’t appear into pending changes rather they appeared as new files, that means it will  delete old “.TAB” files and check-in these new “.SQL” files and lose History associated.

MSDN also states, “You should avoid renaming items managed by TFVC using your operating system (for example, using Windows File Explorer, or the rename command in the Windows command prompt). When you have used your operating system to rename an item in a local workspace, Visual Studio detects the change as two changes: an add and a delete.”

On MSDN page I found out “tf rename” command but the only problem is, it does not take any wildcards and surely you don’t want to do manual rename for 2000+ files. So, I  wrote a power shell script to rename all files.

param($dir, $find, $replace);

######################################
#Renames the old path to the new path
######################################
function Rename($old, $new)
{
#show progress
[Console]::Write(‘.’);

##TFS Rename
$tf = “~\Microsoft Visual Studio\2019\Professional\Common7\IDE\ CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe”;
& $tf rename /lock:none $old $new;
}

## Filter
$includedFiles = ‘*.tab’, ‘*.TAB’;

##rename files
echo “`n# Renaming files:”
dir $dir -r -include $includedFiles | ?{!$_.PsIsContainer -and $_.Name -like “*$find*”} | %{ $newname=$_.Directory.Fullname + ‘\’ + ($_.Name -replace $find, $replace); Rename $_.Fullname $newname }

#DONE!
echo “`nCOMPLETE!`n”;

I hope it saves your precious time.

One thought on “Mass Rename TFS Source Control Files

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.