From 61c2bdaccabee7b982b19d9ad2dff1b0e65f6b1d Mon Sep 17 00:00:00 2001 From: NilsGrunwald Date: Sun, 7 Nov 2021 16:52:03 +0100 Subject: [PATCH] Added check, if folder already exists --- Backup2ExternalHD.ps1 | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Backup2ExternalHD.ps1 b/Backup2ExternalHD.ps1 index 03c8466..a6c5c60 100644 --- a/Backup2ExternalHD.ps1 +++ b/Backup2ExternalHD.ps1 @@ -38,12 +38,24 @@ function Backup-Data($SourcePath,$BackupPath){ $free = (Get-PSDrive -Name $BackupDrive).Free / 1Gb Write-Host(" Backup-Data-Size: $foldersize (free: $free)") if ($foldersize -lt $free){ - $Destination = $BackupPath+"\"+$f - Write-Host(" Source-Path: $Source") - Write-Host(" Target-Path: $Destination") - Copy-Dir -Source $Source -Destination $Destination + $Destination = $BackupPath+"\"+$f + if (Test-Path -Path $Destination){ + Write-Host(" Target folder exists...checking size") + $copysize = (Get-ChildItem -path $Destination -Recurse | Measure-Object -Property Length -sum).Sum / 1Gb + } else { + $copysize = 0 + } + if ($foldersize -ne $copysize){ + Write-Host(" Source-Path: $Source") + Write-Host(" Target-Path: $Destination") + #Copy-Dir -Source $Source -Destination $Destination + } else { + Write-Host(" Skipping $Source, as it is already available on destination with identical size.") + } + if ($move){ - Remove-Item -Recurse -Force $Source + Write-Host(" Finally cleaning up after move") + #Remove-Item -Recurse -Force $Source } } else { Write-Host("ERROR: Not enough free space on Backup-Drive!")