Retrieving the full name of files, filtered by date

后端 未结 1 539
既然无缘
既然无缘 2021-01-24 16:37
$date = [datetime](\"05/19/2014\")
gci -Recurse | Select-Object FullName,LastWriteTime | Where-Object { $_.LastWriteTime.ToShortDateString() -ge $date.ToShortDateString(         


        
相关标签:
1条回答
  • 2021-01-24 17:23

    The issue may be in how you are doing your comparison. The DateTime.ToShortDateString method uses whatever the short date pattern for the current culture your process is using. Unfortunately, I can't think of an example that would make a date in 2009 appear to be greater than a date in 2014 in string form. Try just comparing the date-time object directly. You could just compare dates by using the Date property as such:

    $date = [datetime]("05/19/2014")
    gci -Recurse | Select-Object FullName,LastWriteTime | Where-Object { $_.LastWriteTime.Date -ge $date } | Format-Table -AutoSize
    

    Note that I did not need to use the Date property on your $date variable, as when instantiating a DateTime object with only date information, the time is set to 12:00 AM. The Date property will provide a value with the date of the original DateTime object but with the time set to 12:00 AM.

    The -AutoSize parameter I added to Format-Table will take care of your second question, as it will automatically resize columns to a sane width.

    0 讨论(0)
提交回复
热议问题