PowerShell あるフォルダ配下のプロジェクトファイル(csproj)に含まれないC#(.cs)ファイルを探す
システムの規模が大きくなると、昔は使っていたけど不要になったクラスファイルがVisual Studioで管理されていないけどWindowsのフォルダに物理的に残っちゃうことがよくあります。 そんな不要なファイルを探すPowerShellスクリプトはこちら。
// あるディレクトリ内でcsproj内で参照されている csファイルを抽出 完全ではないが、まぁ、なんとなく。
Get-ChildItem -Path "パス名" *.csproj -Recurse | Select-String -Pattern ".cs""" -Encoding default -CaseSensitive | %{ $_.line.Split('"')[1] }
// あるディクレクトリ内のすべてのcsファイルを抽出
$(Get-ChildItem -Path "パス名" *.cs -Recurse ).FullName
1行で書きたかったけど断念。 PowerShellでLINQしたいけど。出来そうで出来ない。多分できるけど頑張るほどでもない感じ。
$path = "パス名" $a = Get-ChildItem -Path $path *.csproj -Recurse | Select-String -Pattern ".cs""" -Encoding default -CaseSensitive | %{ $_.line .Split('"' )[1 ] } $b = $(Get-ChildItem -Path $path *.cs -Recurse ). FullName foreach( $filename in $b ) { $flg = $true; foreach( $aname in $a ) { if( $filename .Contains($aname ) ) { $flg = $false; break; } } if( $flg ) { Write-Output $filename } }