Lately I’ve been migrating some customers to Office 365.
One of them wanted to migrate their current file structure to sharepoint. Before disposing of their current file server.
Turns out it was quit allot of documents so a manual upload was not an option…
I tried mapping the sharepoint library as a network drive and copy the content but this resulted in many errors about too long filenames and invalid characters.
So I searched the web and found this great powershell script that will rename all the failing maps and files for me.
function Check-IllegalCharacters ($Path, [switch]$Fix, [switch]$Verbose)
{
Write-Host Checking files in $Path, please wait…
#Get all files and folders under the path specified
$items = Get-ChildItem -Path $Path -Recurse
foreach ($item in $items)
{
#Check if the item is a file or a folder
if ($item.PSIsContainer) { $type = “Folder” }
else { $type = “File” }
#Report item has been found if verbose mode is selected
if ($Verbose) { Write-Host Found a $type called $item.FullName }
#Check if item name is 128 characters or more in length
if ($item.Name.Length -gt 127)
{
Write-Host $type $item.Name is 128 characters or over and will need to be truncated -ForegroundColor Red
}
else
{
#Got this from http://powershell.com/cs/blogs/tips/archive/2011/05/20/finding-multiple-regex-matches.aspx
$illegalChars = ‘[&{}~#%]‘
filter Matches($illegalChars)
{
$item.Name | Select-String -AllMatches $illegalChars |
Select-Object -ExpandProperty Matches
Select-Object -ExpandProperty Values
}
#Replace illegal characters with legal characters where found
$newFileName = $item.Name
Matches $illegalChars | ForEach-Object {
Write-Host $type $item.FullName has the illegal character $_.Value -ForegroundColor Red
#These characters may be used on the file system but not SharePoint
if ($_.Value -match “&”) { $newFileName = ($newFileName -replace “&”, “and”) }
if ($_.Value -match “{“) { $newFileName = ($newFileName -replace “{“, “(“) }
if ($_.Value -match “}”) { $newFileName = ($newFileName -replace “}”, “)”) }
if ($_.Value -match “~”) { $newFileName = ($newFileName -replace “~”, “-”) }
if ($_.Value -match “#”) { $newFileName = ($newFileName -replace “#”, “”) }
if ($_.Value -match “%”) { $newFileName = ($newFileName -replace “%”, “”) }
}
#Check for start, end and double periods
if ($newFileName.StartsWith(“.”)) { Write-Host $type $item.FullName starts with a period -ForegroundColor red }
while ($newFileName.StartsWith(“.”)) { $newFileName = $newFileName.TrimStart(“.”) }
if ($newFileName.EndsWith(“.”)) { Write-Host $type $item.FullName ends with a period -ForegroundColor Red }
while ($newFileName.EndsWith(“.”)) { $newFileName = $newFileName.TrimEnd(“.”) }
if ($newFileName.Contains(“..”)) { Write-Host $type $item.FullName contains double periods -ForegroundColor red }
while ($newFileName.Contains(“..”)) { $newFileName = $newFileName.Replace(“..”, “.”) }
#Fix file and folder names if found and the Fix switch is specified
if (($newFileName -ne $item.Name) -and ($Fix))
{
Rename-Item $item.FullName -NewName ($newFileName)
Write-Host $type $item.Name has been changed to $newFileName -ForegroundColor Blue
}
}
}
}
Paste this in your PowerShell and add
Check-IllegalCharacters -Path C:\YourFolder -Fix
Run it without the -Fix option if you just want a list of files and folders to be renamed.
Once this is done you just need to copy the content to your document library. I mapped the library as network drive S: (described http://hitechstorm.com/?p=24) and then used Robocopy to move my files.
This is the command I used: robocopy C:\YourFolder S:\ /E /MOVE /R:2 /LOG:C:\Users\MyUser\Desktop\RobocopyToSharepoint.txt
This will actually move the files and folders to the sharepoint library removing it from my HDD. Retry twice if it fails on a file then move on. And also post the results in a textfile on my Desktop.
PowerShell script found on http://get-spscripts.com/ <–Great site






