Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

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" }

Monday, 24 February 2020

Images used in web project

I am doing little clean-up in the project. Need to identify what images are still used and the rest I would like to remove. This PowerShell script helps to identify images which are used in any page, user control or c# code.

For each in the application is executed command which tries to find its name in all files matching given pattern.


@(Get-ChildItem 'D:\WebApp\images') | 
    ForEach-Object { 
        $_.name | Write-Host -NoNewline
        Write-Host ' ' -NoNewline
        
        dir 'D:\WebApp' -I *.aspx,*.ascx,*.cs,*.master -R | 
            Select-String $_.name -Quiet | where  {$_ -eq $true} | Write-Host -NoNewline
        
        Write-Host #writes a new line
    }