Comparing dates and then archive files greater than 14 days

This topic contains 4 replies, has 5 voices, and was last updated by Profile photo of Sebastian Neumann Sebastian Neumann 1 year, 4 months ago.

  • Author
    Posts
  • #11427
    Profile photo of Mark Clark
    Mark Clark
    Participant

    Hello all
    Need some help please. I need to retrieve the contents of various folders and then check the lastWriteTime. If it is greater than 14 days old I need to move the item into an archive folder. I am getting stuck on comparing the dates and then moving the files. This is what I currently have

    $date = get-date
    $PathToGo = "F:\Scripts\Paygate\Archive"

    write-host "Now connecting to remote storage location"
    start-sleep 3
    #new-psdrive -name PayGate -psProvider FileSystem -Root F:\Scripts\Paygate
    Get-PSDrive -name PayGate -PSProvider FileSystem

    [int]$Count = 0
    $Location = (get-psdrive Paygate).root
    $SearchFolders = Get-ChildItem F:\Scripts\Paygate -Recurse | ForEach {$_.FullName}
    $PaymentList = $SearchFolders
    [int]$TotalPayment = ($SearchFolders | Measure-object).count
    write-host "There are a total of $TotalPayment found."

    $lastwriteTime = (Get-Item $PaymentList).LastWriteTime
    write-host $lastWriteTime

    Compare-Object $b $date

    ForEach ($Payment in $PaymentList)
    {
    copy-item $payment $PathToGo
    }

  • #11433
    Profile photo of Don Jones
    Don Jones
    Keymaster

    If you cast a date time string as an actual datetime object, you can compare them.

    [datetime]$x = "11/22/1971 10:45 AM"
    $y = Get-Date

    if ($x -gt $y) {
    #whatever
    }

    I believe the date you're looking at on the file object is already a date/time, so you should just be able to do a straight comparison.

  • #11659
    Profile photo of TTAction
    TTAction
    Participant

    Maybe this helps:
    To get a variable "from now minus 14 days" :

    $x = (get-date).adddays(-14)

    Maybe this helps

  • #33404
    Profile photo of Kurt Ingram
    Kurt Ingram
    Participant

    I know this post is old but I seem to be getting a strange result when comparing two dates.

    $a = "12/18/2015"
    $b = (get-date).adddays(-90) #would be 10/3/15 using today's date of 12/31/15
    if($a -gt $b){$a}else{$b}

    returns $a.

    Why? Shouldn't it return $b as 12/18/15 is not 'older' than 10/3/15? I'm looking for accounts last modified 90 days from today.

  • #33405
    Profile photo of Sebastian Neumann
    Sebastian Neumann
    Participant

    works for me:

    $sampledate = Get-Date "18.12.2015"
    $nowminus90 = (Get-Date).adddays(-90) #would be 10/3/15 using today's date of 12/31/15
    
    
    if($sampledate -gt $nowminus90){"sampledate: $sampledate"}else{"now: $nowminus90"}
    
    returns: sampledate: 12/18/2015 00:00:00
    

You must be logged in to reply to this topic.