Compare folders and delete dublicates at sourch

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Rickard Sjoberg Rickard Sjoberg 7 months, 2 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #35258
    Profile photo of Rickard Sjoberg
    Rickard Sjoberg
    Participant

    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.

    #35266
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

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

    #35280
    Profile photo of Yuan Li
    Yuan Li
    Participant

    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
    }

    }

    #35464
    Profile photo of Rickard Sjoberg
    Rickard Sjoberg
    Participant

    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"
            
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.