I have a requirement to list out all of the objects that have been deleted from a folder in VSTS using the TFS version control system (ie: not Git).
I can use PowerShell to run the CLI command
tf dir $/path/to/folder /deleted | Out-File -FilePath /path/to/output.txt
However, this includes undeleted files in addition to deleted files. Is there any way to list only files that were deleted from version control, and not undeleted files currently in version control?
With /deleted
in the tf command, it will display deleted items and existing items. The deleted items are followed with ;Xn
where n is the deletion ID
.
We can add a filter to match the ;Xn
to get the deleted files. Sample code below:
# Run the tf command to list all items including deleted ones
tf dir $/path/to/folder /deleted | Out-File -FilePath /path/to/output.txt
# Read the output file and filter only the deleted items, then extract the file names
Get-Content /path/to/output.txt | Where-Object { $_ -match ";X\d+$" } | ForEach-Object { $_ -replace ";X\d+$", "" } | Out-File -FilePath /path/to/deleted_files.txt
In this script, ;X\d+$
is a regular expression that matches any line ending with ;X followed by one or more digits. This will ensure that all deleted files, regardless of the number following ;X, are captured. And also remove :Xn for the deleted files in the list with replacement.