检索文件的全名,按日期过滤 [英] Retrieving the full name of files, filtered by date

查看:29
本文介绍了检索文件的全名,按日期过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在尝试查找在 2014 年 5 月 19 日或之后更改的文件列表.这里发生了两件事:

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. 我收到的文件早在 2009 年就已被更改
  2. 表格的列非常宽,我会说大约 100 个字符左右(我可能会错得非常仔细)

如何获得正确的文件列表,另外,如何以可读的方式对它们进行排序?

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

推荐答案

问题可能在于您如何进行比较.DateTime.ToShortDateString 方法使用您的进程正在使用的当前文化的任何短日期模式.不幸的是,我想不出一个例子,可以使 2009 年的日期以字符串形式显示大于 2014 年的日期.尝试直接比较日期时间对象.您可以使用 Date 属性来比较日期:

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

请注意,我不需要在 $date 变量上使用 Date 属性,就像在实例化一个只有日期的 DateTime 对象时一样信息,时间设置为 12:00 AM.Date 属性将提供一个值,该值具有原始 DateTime 对象的日期,但时间设置为 12:00 AM.

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.

我添加到 Format-Table-AutoSize 参数将解决您的第二个问题,因为它会自动将列大小调整为合理的宽度.

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.

这篇关于检索文件的全名,按日期过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆