There are times where you may need to list the paths to files and folders which exceed a given number of characters. FAT, NTFS, CDFS, ISO DVD, FATX, and Novell Netware all have different limitations on the length of a path. It is helpful to know the paths that will exceed these limits when copying from one system to the other.
I have written a Powershell function below that will scan a path recursively for path lengths that are longer than a user specified length. All paths that exceed that length are written to the screen and to a file. The user can specify the file, and can also let the function know if that file should be overwritten or concatenaed with the new scan data.
Function Find-LongPaths ([string]$path = "C:\" `
, [int]$maxLength = 255 `
, [string]$outputFile = "C:\LongPaths.txt" `
, [bool]$concatenateOutput = $False) {
If ($concatenateOutput -eq $False `
-and (Test-Path $outputFile) -eq $True) {
Remove-Item $outputFile
}
$ofs = "" # By default, the output format separator is a single space,
# and all string concatenations get this space between them!
[string[]]$PathsOverMaxLen = $null
[int]$PathsProcessed = 0
[int]$PathsOverLength = 0
cls
0..7 | %{Write-Host " "}
Write-Host "Paths over $maxLength chars long"
Write-Host "============================="
Write-Host " "
Write-Host "Len Path"
Write-Host "--- -------------------------"
dir -path $path -recurse |
Foreach-Object {
If ($_.FullName.Length -gt $maxLength) {
$PathsOverLength++
Write-Host $_.FullName.Length.ToString().PadLeft(3, " ") $_.FullName
$PathsOverMaxLen += $_.FullName.Trim() + "`r`n"
}
$PathsProcessed++
If ($PathsProcessed % 250 -eq 0) {
Write-Progress `
-activity "Finding paths in $path over $maxLength chars" `
-Status "Processed $PathsProcessed paths" `
-curr "Paths over $maxLength chars in length = $PathsOverLength"
}
}
[string]$FileContent = "`r`n`r`n" `
+ [System.DateTime]::Now.ToString() `
+ "`r`n" + $PathsOverMaxLen
$FileContent >> $outputFile
Write-Host " "
Write-Host "Output written to $outputFile"
Write-Host " "
}
Please be aware that I have split a lot of the code lines above onto multiple lines using the tilde "`" Powershell line continuation character. This is only for the purpose of fitting the code within the available width of this blog. You may remove them if you like.
As you can see, all arguments have default values, or you can specify your own values when executing the function.
The function uses the Write-Progress cmdlet in Powershell so that you can get an idea of the progress of a large scan. If it is a very quick scan, this progress display may not appear.
Posted
May 30 2007, 08:38 PM
by
David