Created
October 7, 2014 11:57
-
-
Save rposbo/8d55b8f76572145a8889 to your computer and use it in GitHub Desktop.
Delete unreferenced images using Powershell
This file contains hidden or 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
# cd to your project directory | |
cd "d:\my luverly project\" | |
# get all the images | |
$images = Get-ChildItem "d:\my luverly project\img\" -Exclude "*.config" | |
# for each image.. | |
foreach($img in $images){ | |
$found = "" | |
# look in css, js, cshtml files for a match | |
foreach($file in Get-ChildItem -Include "*.css", "*.js","*.cshtml" -Recurse){ | |
# got a match! | |
if (Select-String -pattern $img.Name -SimpleMatch -Quiet -Path $file.FullName){ | |
$found = $img.Name | |
} | |
} | |
# no match? delete the image | |
if ($found -ne $img.Name){ | |
Remove-Item $img.FullName | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment