Wednesday 13 March 2024

Identifying invalid resx files in web project

Recently I needed to do a bit of cleanup as in one project after many UI changes, where we moved or changed structure of pages, were left resource files which no longer had corresponding page or user control. 

Nobody likes doing such cleanup manually so I come up with short powershell script which went though individual resource files and try to identify if it is still needed or not.

Explanation:

  1. Find all files in folder with local resources (e.g. workflow.aspx.en-US.resx)
  2. For each file remove the suffix e.g. en-US.resx
  3. Remove the subdirectory from name in order to get name of the page worflow.aspx
  4. Get each file just once (there can be multiple language mutations)
  5. Check if the file exists
  6. Write out only those where the file was not found

$res_location = "./App_LocalResources/"
Get-ChildItem -Path $res_location -Name 
| ForEach-Object -Process {"$res_location$_" -ireplace "(.[a-z]{2}-[a-z]{2})?.resx",""} 
| ForEach-Object -Process {$_.Replace($res_location, "")} 
| Get-Unique 
| ForEach-Object -Process {Write-Output "$_ $(Test-Path $_ -PathType Leaf)"} 
| where { $_ -match "False" }