使用命令行按日期搜索 [英] Search by date using command line
问题描述
有什么方法可以根据日期搜索目录中的文件吗?我想找到所有创建日期大于特定日期的文件,是否可以使用 dir
命令?
dir / code>使用为
命令。如果在您的国家 dir
以YMD格式打印日期,则只需要将其与给定日期进行比较。如果日期部分的顺序不同,那么必须使用另一个命令来解析日期并将其更改为YMD。这将显示第5次使用后修改的文件列表。 @Echo Off
for / f usebackq tokens = 1,4 skip = 5%% A in(`dir / -c`)do(
if %% A GTR 2012-02-05 echo %% A %% B
)
如果
在结束时,如果汇总行通过比较,您可以获得额外的行。要避免它,你可以使用 if %% A GTR 2012-02-05 if exists %% B echo %% A %% B
编辑:
有更好的方法避免解析 dir
输出,并且允许按时间搜索,而不仅仅是日期: / p>
@Echo Off
for / r %% A in(*)do(
if%%〜tAGTR2012-02-05 00:00echo %%〜tA %% A
)
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?
解决方案 dir
by itself can not filter by date, but you can parse the output of dir
using for
command. If in your country dir
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 another for
command to parse the date and change it to YMD. This will display a list of files modified after 5th Februrary.
@Echo Off
for /f "usebackq tokens=1,4 skip=5" %%A in (`dir /-c`) do (
if %%A GTR 2012-02-05 echo %%A %%B
)
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 use if %%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:
@Echo Off
for /r %%A in (*) do (
if "%%~tA" GTR "2012-02-05 00:00" echo %%~tA %%A
)
这篇关于使用命令行按日期搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!