Created
May 18, 2023 09:31
-
-
Save jonashammerschmidt/9c83777154def11dcbb072340fa859a1 to your computer and use it in GitHub Desktop.
Removing Unused Attachments (Images) from Azure DevOps Wiki
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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