0KB Files in SharePoint Online – ShareGate Migration

0KB Files in SharePoint Online – ShareGate Migration

Issue: Opening a 0KB file in Excel produces the error “The workbook cannot be opened” via Web. On Desktop, it says, “Excel cannot open the file X because the file format or file extension is not valid,” Other files like Docx and PPTx appear blank when opened for the first time and don’t throw errors but will save the file and update the size.

The file Size in SharePoint is blank for these files:

Solution:Β In our scenario, the problem was caused by copying with ShareGate. Every piece of file migration software has some issues, some worse than others, but you should never fully trust this kind of software without testing the results. Unfortunately, this copy job in ‘Insane’ mode seems to have created 0KB empty files, and since the file now exists, and the timestamps align, other copy jobs aren’t looking at the hash or size of the files and skipping them. This is a nightmare, given there are many files and sub-folders to go through, so I’ve devised a solution to recursively identify these 0KB files within all Document Libraries in a Site Collection.

Scan0KBFiles.PS1

# Checks all Document Libraries within the Site Collection recursively

$allSites = @("https://yoursite.sharepoint.com/sites/reports", "SITEURL2")

# File is automatically created in this directory. Specify a path to write it
$tempFolder = "C:\Users\YourProfile\"

function Scan-site {
param (
[Parameter(Mandatory)]
[string]$siteUrl
)

write-host "connecting"
Connect-PnPOnline -Url $siteUrl -Interactive
$DocumentLibraries = Get-PnPList -Includes DefaultDisplayFormUrl | Where-Object { $_.BaseTemplate -eq 101 -and $_.DefaultDisplayFormUrl -notlike "*Style Library*" -and $_.DefaultDisplayFormUrl -notlike "*FormServerTemplates*" -and $_.DefaultDisplayFormUrl -notlike "*SiteAssets*" }

$context = Get-PnPContext

$DocumentLibraries | % {
$currentLibrary = $_
$context.Load($currentLibrary.RootFolder)
$context.Load($currentLibrary.RootFolder.Folders)
$context.ExecuteQuery()
#work on the root folder
Scan-folder -Folder $currentLibrary.RootFolder -siteCollectionUrl $siteUrl
#work on the first-level folders
$currentLibrary.RootFolder.Folders | % {
Scan-folder -Folder $_ -siteCollectionUrl $siteUrl
}

}

}

function Scan-folder {
param (
[Parameter(Mandatory)]
$Folder,
[Parameter(Mandatory)]
$siteCollectionUrl
)
write-host "working on folder " $Folder.ServerRelativeUrl
$context.Load($Folder.Files)
$context.Load($Folder.Folders)
$context.ExecuteQuery()
$files = $Folder.Files
$files | % {
$currentFile = $_
if ($currentFile.Length -eq 0) {
Add-Content -Path $filePath -Value ("{0};{1}" -f $siteCollectionUrl, $currentFile.ServerRelativeUrl)
Write-Host $currentFile.ServerRelativeUrl " size : " $currentFile.Length -BackgroundColor Red
}

}
write-host "working on subfolders of " $Folder.ServerRelativeUrl
$Folder.Folders | % {
Scan-folder $_ -siteCollectionUrl $siteCollectionUrl
}
}

$fileName = "{0}.csv" -f (Get-Date).Ticks
$filePath = "{0}{1}" -f $tempFolder, $fileName

Set-Content -Path $filePath -Value "SiteCollectinURL;FilePath"

$allSites | % {
Scan-site -siteUrl $_
}

You’ll end up with a randomly named CSV file with all of the 0KB files listed when the scan completes. Ignore what’s on the console unless you want to keep an eye on things, but capturing that data won’t be necessary as it scrolls in PowerShell.

If you get errors, make sure you have done Install-Module MSOnline, AzureAD, and Microsoft.Online.SharePoint.PowerShell, SharePointPnPPowerShellOnline, and all modules are on the latest versions if they already exist on the box.

How to Install the PnP PowerShell Module

If you’re seeing Throttling, I do have a version of this script that works as an AzureAD registered app, versus using credential authentication.

I tried testing other migration tools, all the ones you’d find on Google, and it’s pretty bad out there right now. If they can even hook into your tenant, the UIs are wonky, and you get none of the visibility or configurability in other tools. But, now, amongst many other bugs, you can see that in the case of ShareGate, that 5k gets you problems like this one, which admittedly, are a nightmare considering there are over 1/4 million files to search through looking for these 0KB stubs that have replaced valid data due to the bug.

Leave a Reply

Your email address will not be published.