# Compare folders and delete dublicates at sourch

**URL:** <https://forums.powershell.org/t/compare-folders-and-delete-dublicates-at-sourch/5781>\
**Category:** PowerShell Help\
**Created:** [February 14, 2016, 9:19pm UTC](https://forums.powershell.org/t/compare-folders-and-delete-dublicates-at-sourch/5781 "2016-02-14T21:19:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rickard-sjoberg](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@rickard-sjoberg](https://forums.powershell.org/u/rickard-sjoberg)\
**Post date:** [February 14, 2016, 9:19pm UTC](https://forums.powershell.org/t/compare-folders-and-delete-dublicates-at-sourch/5781/1 "2016-02-14T21:19:17Z")

</div>

Hi

I have an issue, where I have a folder with a large number of images,  
These images will be reduced in size by a batch job and be saved to another location.

Now i would like to compare the filenames in both locations and then delete the source files if they are older then x number of days.

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [February 15, 2016, 1:05am UTC](https://forums.powershell.org/t/compare-folders-and-delete-dublicates-at-sourch/5781/2 "2016-02-15T01:05:53Z")

</div>

Have you done anything on this at all? Please post your script.

---

<div class="post-metadata">

**Author:** ![yuan-li](https://avatars.discourse-cdn.com/v4/letter/y/aeb1de/32.png) [@yuan-li](https://forums.powershell.org/u/yuan-li)\
**Post date:** [February 15, 2016, 9:13am UTC](https://forums.powershell.org/t/compare-folders-and-delete-dublicates-at-sourch/5781/3 "2016-02-15T09:13:38Z")

</div>

Just some pseudocode and hopefully it can help you to start

$souredir=“c:\source”  
$destdir=“c:\dest”  
$a=ls c:\source  
$b=ls c:\dest

foreach ( $c in $a) {

$sourcefile=$source+$c.name  
$destfile=$destdir+$c.name

if ( test-path $destfile ){

```
$destfiletime=Get-ItemProperty $destfile
if($c.lastwritetime -lt ($destfiletime.AddDays(-10))){
  remove-item $sourcefile

}

```

}else{

```
#batch job and copy $c to the destination folder

```

}

}

---

<div class="post-metadata">

**Author:** ![rickard-sjoberg](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@rickard-sjoberg](https://forums.powershell.org/u/rickard-sjoberg)\
**Post date:** [February 18, 2016, 8:00pm UTC](https://forums.powershell.org/t/compare-folders-and-delete-dublicates-at-sourch/5781/4 "2016-02-18T20:00:31Z")

</div>

I managed to solve part of my problem, but it seems like it only compares the first level folder in the directories and not the files. And so far i have not implemented the date check yet.

```
$source = Get-ChildItem Y:\sourcedir
$dest = Get-ChildItem V:\destinationdir

$matches = (Compare-Object -ReferenceObject $source -DifferenceObject $dest -Property Name -ExcludeDifferent -IncludeEqual)

foreach ($file in $matches)
{
        write-host "Deleting file $File"
        
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:42pm UTC](https://forums.powershell.org/t/compare-folders-and-delete-dublicates-at-sourch/5781/5 "2024-05-16T20:42:51Z")

</div>


