Reputation: 1095
I try to compare 2 Folders. A and B. B is a Backup Folder of A. Firstly I want to check if any Files have been deleted in Folder A, if so, I want to take that corresponding File from B and move it. Then I can go ahead and RoboCopy /mir A again which will create the Backup.
My issue is how to compare the two directories (A and B have subfolders etc), and then move the Deleted File. Here is the Code which is supposed to do this task.
#region | Global Variables
$backupFolder = "Sandbox_Backup"
$UsbDisk = "C:\Users\pullrich\Desktop"
#endregion
$date = Get-Date -Format G
# Create Log that Script has been started
MyLog "--- Script has been started at $date ---" 0
$testfolder = Test-Path "$UsbDisk\$backupFolder"
# If a Backup folder already exist then Compare files and see if any changes have been made
if ( $testfolder -eq $true ) { #IF_2
# Copy deleted files to BackUp Folder
MyLog "Check for Deleted Files on Data_01:\" 0
$source = $testfolder + "\Data_01"
$sourcelist = Get-ChildItem $source -Recurse
$destination = "$UsbDisk\$backupFolder\Data_01\_DeletedFiles"
$testDestination = Test-Path $destination
if ( $testDestination -eq $false ){
mkdir $destination
}
foreach ($file in $sourcelist){
$result = test-path -path "C:\Users\pullrich\Desktop\Sandbox\Data_01*" -include $file.Name
if ( -not $result ){
$CopyFile = $file.DirectoryName + "\" + $file.Name
Copy-Item $CopyFile -Destination $destination
Remove-Item $CopyFile
}
}
# Start Synchronizing Data_01:\
MyLog "Start Synchronizing Data_01:\" 0
Robocopy "C:\Users\pullrich\Desktop\HP-Sandbox\Data_01" "$UsbDisk\$backupFolder\Data_01" /mir /r:2 /w:3 /M /XD VM_*
MyLog "Data_01:\ is up to Date" 0
} #IF_2
UPDATE 1
I kept working on the Code and figured out that the issue seems to be here:
$result = test-path -path "C:\Users\pullrich\Desktop\Sandbox\Data_01*" -include $file.Name
It will always return false, because it is not really doing what I intended it to do I guess. It's supposed to go through every File in the B folder and check if that File exists in A.
Answer
I was able to get the Script done with latkins' answer. For reference, here is the Full script.
#region | Global Variables
$backupFolder = "Sandbox_Backup"
$UsbDisk = "C:\Users\pullrich\Desktop"
#endregion
$date = Get-Date -Format G
# Create Log that Script has been started
MyLog "--- Script has been started at $date ---" 0
$testfolder = Test-Path "$UsbDisk\$backupFolder"
# If a Backup folder already exist then Compare files and see if any changes have been made
if ( $testfolder -eq $true ) { #IF_2
# Copy deleted files to BackUp Folder
MyLog "Check for Deleted Files on Data_01:\" 0
# Set Data_01 as Source
$source = "$UsbDisk\$backupFolder\Data_01"
# Get all Items
$sourcelist = Get-ChildItem $source -Recurse
# Choose as Destination the DeletedFiles Folder
$destination = "C:\Users\pullrich\Desktop\Sandbox\Data_01\_DeletedFiles"
mkdir $destination
# Check if a File or Folder exists in the BackUp folder but not on the Server
foreach ($file in $sourcelist){
# First we have to check if the File we have in the Backup still exists on the Server; however, we do not care about the DeletedFiles Folder
if ( -not ($file.Name -eq "_DeletedFiles" )){
$fullPath = "C:\Users\pullrich\Desktop\Sandbox\Data_01" + $file.FullName.Substring($source.Length)
$result = test-path $fullPath
# If it doesn't exist then we go ahead and Copy the File to the DeletedFiles Folder on the Server,
# which will later be copied to the BackUp and then deleted of the Server
if(-not $result ){
$CopyFile = $source + $file.FullName.Substring($source.Length)
Copy-Item $CopyFile -Destination $destination -force
}
}
}
# Start Synchronizing Data_01:\
MyLog "Start Synchronizing Data_01:\" 0
Robocopy "C:\Users\pullrich\Desktop\Sandbox\Data_01" "$UsbDisk\$backupFolder\Data_01" /mir /r:2 /w:3 /M /XD VM_*
#Delete the DeletedFiles Folder from Server and keep it only on the BackUp
Remove-Item $destination
MyLog "Data_01:\ is up to Date" 0
} #IF_2
else { #Else_2
mkdir "$UsbDisk\$backupFolder"
# Start Copying Data
MyLog "Start Backing up Data_01:\" 0
Robocopy "C:\Users\pullrich\Desktop\Sandbox\Data_01" "$UsbDisk\$backupFolder\Data_01" /mir /r:2 /w:3 /XD VM_*
}
# Delete All Files in _DeleteFiles Folder which are Older than 60 Days
Upvotes: 0
Views: 1007
Reputation: 16792
You could construct the full path for each potential file, rather than relying on a potentially costly recursive wildcard search.
Each $file
comes from some subdirectory of $source
, so the full path (given by $file.FullName
) can be shortened to the "relative" path by dropping the first $source.Length
chars from $file.FullName
. Take your new root path and add it to this relative path, and you've got the new full path.
$fullPath = "C:\Users\pullrich\Desktop\Sandbox\Data_01" + $file.FullName.Substring($source.Length)
$result = Test-Path $fullPath
Upvotes: 1