SteveMustafa
SteveMustafa

Reputation: 619

Retrieving the full name of files, filtered by date

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

I'm trying to find the list of files that have been altered on or after the 19th of May, 2014. Two things are happening here:

  1. I'm getting files that have been altered from as far back as 2009
  2. The table's columns are very wide, I'd say about 100 or so characters (I could be very wrong as I am eyeballing this)

How can I get the proper list of files and additionally, how can I sort them in such a manner as they are readable?

Upvotes: 1

Views: 339

Answers (1)

Joseph Alcorn
Joseph Alcorn

Reputation: 2442

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.

Upvotes: 2

Related Questions