# How To Weed out long paths easily ahead of a OneDrive for Business migration

You know exactly what’s happened as soon as you see it. That pesky error. One of your users has managed to create a directory structure so long courtesy of a sneaky mapped drive and now you’re unable to do anything with it; least of all get it synchronising with OneDrive for Business. If only there was a way to weed out those long paths in advance and fix them before people encounter the error?

Helpfully, there are some options to make this easier. I went scouring the web for something free, simple and easy to find all files where the path length would exceed the limit for OneDrive for Business (256 chars). A user by the handle “deadlydog” had posted some PowerShell script to Stack Overflow that looked like it might do the job.

In the spirit of building on the great ideas of others, I’ve taken deadlydog’s initial solution and extended it slightly to include a character limit. I figured someone running this script is more interested in the files longer than the limit rather than every file path length. I’ve also altered it to output to a CSV file since that might be more useful when it comes to manipulating the data in Excel or Power BI should you have large numbers of files that exceed the limit.

## Sample PowerShell Script

$pathToScan = "C:\Some\Directory" # The path to scan and the the lengths for (sub-directories will be scanned as well).$outputFilePath = "C:\temp\PathLengths.csv" # This must be a file in a directory that exists and does not require admin rights to write to.
$writeToConsoleAsWell =$false   # Writing to the console will be much slower.
$maxLength = 256 #Max path length. Script will record paths equal to, or longer than this. # Open a new file stream (nice and fast) and write all the paths and their lengths to it.$outputFileDirectory = Split-Path $outputFilePath -Parent if (!(Test-Path$outputFileDirectory)) { New-Item $outputFileDirectory -ItemType Directory }$stream = New-Object System.IO.StreamWriter($outputFilePath,$false)
Get-ChildItem -Path $pathToScan -Recurse -Force | Select-Object -Property FullName, @{Name="FullNameLength";Expression={($_.FullName.Length)}} | Sort-Object -Property FullNameLength -Descending | ForEach-Object {
$filePath =$_.FullName
$length =$_.FullNameLength
$string = "$length,$filePath" # Write to the Console. if ($writeToConsoleAsWell) { Write-Host $string } #Write to the file. if ($length -ge $maxLength) {$stream.WriteLine($string)} }$stream.Close()

By running this with the appropriate permissions you should generate a CSV with a line per file broken out by path length and full path. This will make it easier to identify potential problems when synchronising with OneDrive for Business or SharePoint Online.

Posted

in

by