PowerShell: Synchronizing a Folder (and Sub-Folders) Part 6

Hi. Here’s another update on the Sync-Folder script here.  In this part I add some more code to use literal paths (so that special characters don’t cause the sync to fail) and a report of the numbers of copies, updates, folder removals etc performed.

Update : I’ve revisited this script a few times with new additions and modifications.The latest full version of the script is here.  That post also includes links covering the other revisions to the script.There were only a couple of changes in this version.

    $SourceList=Get-ChildItem -LiteralPath $SourceFolder
    $TargetList=Get-ChildItem -LiteralPath $TargetFolder

This uses -LiteralPath instead of -Path.  That means any ‘special’ characters are treated literally and not interpreted.  It should mean that paths with odd characters in them don’t generate errors.

    Sync-OneFolder $SourceFolder $TargetFolder $Exceptions | Tee-Object -Variable Changes

        Sync-OneFolder $Pair.Source $Pair.Target $Pair.ExceptionList.Exception | Tee-Object -Variable Changes

Here the Tee-Object command was added to these two lines.  The cmdlet takes the pipeline output and splits (“Tees”, like the letter “T”) the pipeline both to the Output and to a file ($Changes).  We can use $Changes to report as below;

Write-Host "Statistics"
Write-Host ("Folder Creations: " +($Changes | ? {$_.Process -eq "Create Folder"}).Count)
Write-Host ("Copies: " +($Changes | ? {$_.Process -eq "Copy File"}).Count)
Write-Host ("File Removals: " +($Changes | ? {$_.Process -eq "Remove File"}).Count)
Write-Host ("Folder Removals: " +($Changes | ? {$_.Process -eq "Remove Folder"}).Count)
Write-Host ("Updates: " +($Changes | ? {$_.Process -eq "Update File"}).Count)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: