使用命令行按日期搜索
有没有办法根据日期搜索目录中的文件?我想查找创建日期大于特定日期的所有文件,是否可以使用 dir 命令来实现?
Is there any way to search for files in a directory based on date? I want to find all files with created date greater than a specific date, is it possible to do it with dir
command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
刚刚发现了
forfiles
命令。将列出所有子目录中超过 7 天修改的所有日志文件,尽管它似乎不查看创建日期。它确实支持指定特定日期。
请参阅
forfiles /?
了解更多信息。Just discovered the
forfiles
command.Will list all the log files modified more than seven days old, in all subdirectories, though it does not appear to look at the create date. It does support specifying a specific date.
See
forfiles /?
for more info.对我来说更简单的方法是这样的
an easier way for me is something like
dir
本身无法按日期过滤,但您可以使用for
命令解析dir
的输出。如果在您所在的国家/地区dir
以 YMD 格式打印日期,那么您只需将其与给定日期进行比较即可。如果日期部分的顺序不同,则必须使用另一个for
命令来解析日期并将其更改为 YMD。这将显示 2 月 5 日之后修改的文件列表。if
执行标准字符串比较,因此如果摘要行通过比较,最后您可以获得附加行。为了避免这种情况,您可以使用if %%A GTR 2012-02-05 if exit %%B echo %%A %%B
编辑:
还有更好的方法,可以避免解析
dir
输出,并且还允许按时间搜索,而不仅仅是按日期搜索:dir
by itself can not filter by date, but you can parse the output ofdir
usingfor
command. If in your countrydir
prints the date in YMD format, then you only need to compare it with given date. If the order of date parts is different, then you have to use anotherfor
command to parse the date and change it to YMD. This will display a list of files modified after 5th Februrary.if
does standard string comparison, so at the end you can get additional line if summary line passes the comparison. To avoid it, you can useif %%A GTR 2012-02-05 if exist %%B echo %%A %%B
EDIT:
There is even better approach which avoids parsing of
dir
output and also allows searching by time, not only by date:嗯,据我所知,你不能,但这种想法会起作用,但仍然毫无用处,除非你的日期范围很短;)
Well you cant as far as i know, but this sort of think will work, but still really useless unless you have a short date range ;)
使用 PowerShell 可以轻松做到这一点。我知道您的问题是关于 cmd 的,但 PS 包含在 Windows 7 及更高版本中。它还可以安装在 XP 和 Vista 上。
使用
Get-ChildItem
命令(别名为dir
)获取所有文件。将输出通过管道传输到Where-Object
命令(别名为?
),以返回日期大于 (-gt
) 特定日期的文件日期。对于 Powershell 2.0(Windows 7 上的默认设置),您必须使用脚本块:
对于 Powershell 3.0(Windows 8 上的默认设置)及更高版本,您可以使用以下更简单的语法:
dir -file
命令返回一个集合System.IO.FileInfo
对象。该文件对象具有许多可用于复杂过滤的属性(文件大小、创建日期等)。有关文档,请参阅 MSDN。This is easy to do with PowerShell. I know that your question was about cmd, but PS is included in windows 7 and later. It can also be installed on XP and Vista.
Use the
Get-ChildItem
command (aliased asdir
) to get all files. Pipe the output to theWhere-Object
command (aliased as?
) to return files where the date is greater then (-gt
) a specific date.For Powershell 2.0 (default on Windows 7), you must use a scriptblock:
For Powershell 3.0 (default on Windows 8) and up you can use this simpler syntax instead:
The
dir -file
command returns a collection ofSystem.IO.FileInfo
objects. This file object has many properties that you can use for complex filtering (file size, creation date, etc.). See MSDN for documentation.