Script Help please -ge not working

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5985

    by scripthelp at 2012-12-18 12:53:47

    I been working on this script and everytime I think I am close to resolution it doesn't preform the way I expect it too.

    $OldFileTime = 365
    Where-Object -Filterscript {$_.lastWriteTime -ge (get-date).addDays(-365) -and $_.creationtime -ge (get-date).addDays(-365) -le $OldfileTime

    I am going to assume I can not compare these dates this way do to format of get-date.

    I basically was looking for the syntax to list files based on lastwritetime greater than 365 days and modification date and list the files. Verification or logging before deleteing these files with remove-item.

    Thank you for the assistance

    by nohandle at 2012-12-18 13:13:07

    Golden rule when delaing with conditions: Use parentheses.

    To be able to compare DateTime objects (last write time is returned as object of System.DateTime type), objects on both sides must be of the same type or convertible to same type. I am not exactly sure what the resulting condition is in your case because this is bit unclear for me: "syntax to list files based on lastwritetime greater than 365 days and modification date and list the files." But to get files that are older that 365 days the condition is following]Get-ChildItem * | where {$_.lastWriteTime -le (Get-Date).AddDays(-365)}[/powershell]Checking for the creation date is unnecessary, the last write time is also set when the creation time is set and file cannot be written before it was created.

    by scripthelp at 2012-12-19 02:30:39

    I want to thank you for the information that you provided. I guess I need to be more clear in what I am trying to do. Please note new to scripting.
    I am writing a script that looks into a folder (FTP Folder) -Recurse.

    I am trying to delete files that were last written to is equal to or greater than (5) and last modified date is equal to or greater than (5) list file and remove it

    $Oldtimefiles =(Get-Date).adddays(-365)
    Get-ChildItem * | Where {$_.lastWRITEtIME -ge (Get-Date).AddDays(-365) -and $_.creationdate -ge (Get-Date).AddDays(-365) -le

    Again Thank you for all your help and explaining it to me.

    by scripthelp at 2012-12-19 02:44:47

    I am sorry you are right my logic of -ge is wrong and I should be using -le
    I believe the script that I just wrote does the following.
    If last write time is le 40 days and creation time is le 40 list files. than if I put Remove-Item at the end of the script is should only delete those that were listed.
    Get-ChildItem * | where {$_.lastWriteTime -le (Get-Date).AddDays(-40) -and $_creationTime -le (get-Date).AddDays(-40)}

    Sorry new to scripting like I said and yes I am testing it as we speak. Thank you

    by nohandle at 2012-12-19 07:04:19

    [quote="scripthelp"]I believe the script that I just wrote does the following.
    If last write time is le 40 days and creation time is le 40 list files.[/quote]
    Well you missed a dot there before the creation time. If you test it still works because checking creation time is unnecessary in this case and the condition you passed always return true.
    $_creationTime -le (get-Date).AddDays(-40)
    This $_creationTime does not exist so it returns $null and
    $null is always less than any datetime object.

    [quote="scripthelp"]If last write time is le 40 day[/quote]
    You create a datetime object (by the get-date) that contains (current) date and time and you use addDays method on it to add "minus 40 days" – you are returned datetime object that contains datum that is 40 days in the past. The Last write time property also returns datetime object. If the datetime object from lastwrite time property contains date that is older than object containing the current date and time the condition is true and the where cmdlet lets the current object ($_) go down the pipeline.
    Get-ChildItem * | where {$_.lastWriteTime -le (Get-Date).AddDays(-40)}
    Again this should be enough to list files in the current directory that has not been written in 40 days.

You must be logged in to reply to this topic.