Compare folders and delete dublicates at sourch

This topic contains 3 replies, has 3 voices, and was last updated by  Rickard Sjoberg 2 years, 5 months ago.

  • Author
  • #35258

    Rickard Sjoberg


    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.

  • #35266

    Bob McCoy

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

  • #35280

    Yuan Li

    Just some pseudocode and hopefully it can help you to start

    $a=ls c:\source
    $b=ls c:\dest

    foreach ( $c in $a) {


    if ( test-path $destfile ){

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



    #batch job and copy $c to the destination folder


  • #35464

    Rickard Sjoberg

    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"

You must be logged in to reply to this topic.