LoginSignup
2
1

More than 5 years have passed since last update.

PowerShell で ファイル文字長制限を考慮したファイル一覧を取得する

Last updated at Posted at 2018-09-16

説明

  • PowerShellでファイル文字長制限(PathToLongException、指定されたパス、ファイル名、またはその両方が長すぎます。)が発生する場合の対策
  • サードパーティーのDLL(http://alphafs.alphaleonis.com/)を利用します。
    • スクリプトと同じフォルダに配置します。
  • 隠し属性のファイルは除いています。
  • サイズによる制限も実行部には記載しています。

コード

関数
function GetRecurseFiles($mypath) {
    <#
    .DESCRIPTION
    255文字制限対策(サードパーティーの情報を使用する)  
    #>
    Import-Module .\AlphaFS.dll
    $fso = New-Object -ComObject Scripting.FileSystemObject 

    $myfi = @()

    [Alphaleonis.Win32.Filesystem.Directory]::GetFiles($mypath, "*", [System.IO.SearchOption]::AllDirectories) | ForEach-Object {
        $longFullName = $_
        $f = $fso.GetFile($longFullName) 
        $shortName = $f.ShortPath
        $onefile = Get-Item -Force -LiteralPath $shortName

        if ($onefile.Attributes -band [System.IO.FileAttributes]::Hidden) {
        }else{
            $myfi += $onefile
        }
    }

    return $myfi 
}
実行部
# フォルダパス
$filepath = "C:\Temp\"
# 50MB以上制限
$maxsize = 1024*50000

# 取得
$fi = GetRecurseFiles($filepath) | Where-Object {$_.Length -ge ($maxsize)}

# 確認
Write-Host $fi.Count
$fi | ForEach-Object {
    Write-Host $_.Name
    Write-Host $_.FullName
    Write-Host $_.Length
}
2
1
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
2
1