LoginSignup
3
3

More than 5 years have passed since last update.

PowerShell あるフォルダ配下のプロジェクトファイル(csproj)に含まれないC#(.cs)ファイルを探す

Last updated at Posted at 2015-03-28

システムの規模が大きくなると、昔は使っていたけど不要になったクラスファイルがVisual Studioで管理されていないけどWindowsのフォルダに物理的に残っちゃうことがよくあります。
そんな不要なファイルを探すPowerShellスクリプトはこちら。

// あるディレクトリ内でcsproj内で参照されている csファイルを抽出 完全ではないが、まぁ、なんとなく。
Get-ChildItem -Path "C:\Users\koki\Documents\Visual Studio 2015\Projects\NLayerAppV2" *.csproj -Recurse | Select-String -Pattern ".cs""" -Encoding default -CaseSensitive | %{ $_.line.Split('"')[1] }

// あるディクレクトリ内のすべてのcsファイルを抽出
$(Get-ChildItem -Path "C:\Users\koki\Documents\Visual Studio 2015\Projects\NLayerAppV2" *.cs -Recurse ).FullName

1行で書きたかったけど断念。
PowerShellでLINQしたいけど。出来そうで出来ない。多分できるけど頑張るほどでもない。

findNoManageCSFiles.ps1
$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
    }
}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3