Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jonashammerschmidt/9c83777154def11dcbb072340fa859a1 to your computer and use it in GitHub Desktop.
Save jonashammerschmidt/9c83777154def11dcbb072340fa859a1 to your computer and use it in GitHub Desktop.
Removing Unused Attachments (Images) from Azure DevOps Wiki
# The root directory of your local repository
$rootDirectory = 'C:\path\to\your\repo'
# The attachments directory
$attachmentsDirectory = Join-Path -Path $rootDirectory -ChildPath '.attachments'
# Get a list of all the image files in the attachments directory
$imageFiles = Get-ChildItem -Path $attachmentsDirectory -File
# Get a list of all the markdown files in the repository
$markdownFiles = Get-ChildItem -Path $rootDirectory -Recurse -Filter *.md
# Iterate over each image file
foreach ($imageFile in $imageFiles) {
# The name of the image file
$imageName = $imageFile.Name
# Assume that the image is not used until we find a reference to it
$imageUsed = $false
# Iterate over each markdown file
foreach ($markdownFile in $markdownFiles) {
# Read the content of the markdown file
$markdownContent = Get-Content -Path $markdownFile.FullName
# Check if the content contains a reference to the image file
if ($markdownContent -match $imageName) {
# The image is used
$imageUsed = $true
# No need to check the other markdown files
break
}
}
# If the image is not used, then delete it
if (-not $imageUsed) {
Remove-Item -Path $imageFile.FullName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment