Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:17:12+00:00 2026-06-12T18:17:12+00:00

I try to compare 2 Folders. A and B . B is a Backup

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:17:14+00:00Added an answer on June 12, 2026 at 6:17 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem when I try to compare two large files. What I
I have used a stacked bar chart (with coord_flip) to try to compare distributions
I have created an SVN repository for project but now realise that the files
I'm uploading some files through the POST in iframe wrappers that might take an
I have n csv files which I need to compare against each other and
I try to compare Mnesia with more traditional databases. As I understand it tables
i try to compare two lists with each other: ListA (a1,a2,a3,...) ListB (b1,b2,b3,...) I
I try to get some website attribute (colour of the cell) and compare in
For example, I try to do something like this: - (BOOL)compare:(NSDecimal)leftOperand greaterThan:(NSDecimal)rightOperand { BOOL
IF gives the wrong answer when I try to compare 2 large numbers. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.