批处理文件来检查文件的修改日期,然后把它比作另一 [英] Batch File to check file's modified date and then compare it to another

查看:176
本文介绍了批处理文件来检查文件的修改日期,然后把它比作另一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个包含了不同的修改日期时间的一些文件。他们中的一些由天不同,有的只用几小时或几分钟。如何访问日期修改时间,然后对它们进行比较,按天进行排序?

Say that I have a few files with that have different date modified times. Some of them differ by days, some only by hours or minutes. How can I access the date modified time and then compare them, and sort them by day?

例如,如果我有这样的一组文件如下:

For example, if I have this set of files below:

Date                    File
2015/07/29  15:31       A1
2015/07/29  16:00       A2
2015/07/30  15:00       B1
2015/07/30  15:05       B2

和我想要的输出是:

2015/07/29  16:00
2015/07/30  15:05

正如你所看到的,我不关心文件的名字,我只是想知道最后的修改日期,每天的时间。

As you can see, I don't care about the file's name, I just want to know the last date modified time per day.

推荐答案

下面的返回唯一修改日期的列表,并为每一天的最晚时间,最近的日期将出现第一个:

The following returns a list of unique modification dates and the latest times for each day, the most recent date appears first:

@echo off
set FOLDER=D:\StackOverflow\BatchTesting
setlocal EnableDelayedExpansion
set PREVIOUS=
for /F "skip=4 tokens=1-2" %%A in ('dir /N /A:-D /O:-D /T:W "%FOLDER%"') do (
    if not "%%B"=="File(s)" (
        if not "%%B"=="Dir(s)" (
            if not "%%A"=="!PREVIOUS!" echo %%A  %%B
            set PREVIOUS=%%A
        )
    )
)
endlocal

说明:

这种方法的核心元素是 DIR 命令,用于由变量给出的文件夹文件夹,其中输出是这样的:

Explanation:

Core element of this approach is the dir command, applied on the folder given by the variable FOLDER, which outputs something like this:

 Volume in drive D has no label.
 Volume Serial Number is 0000-0000

 Directory of D:\StackOverflow\BatchTesting

2015/07/31  00:30                12 foo.txt
2015/07/30  20:15               512 bar.txt
2015/07/30  18:45             1'432 somelog.log
2015/07/30  09:00               573 testscpt.bat
2015/07/28  17:10                99 crapfile.tmp
               5 File(s)          2'628 bytes
               0 Dir(s)  100'000'000'000 bytes free

循环解析 DIR 输出,跳过头和提取前两个(空格分隔)令牌,这是行之后的日期和时间,线。在迪尔选项被设置为使得只有文件列(没有目录),并在相对于该修改日期/时间的降序排序。

The for loop parses the dir output, skipping the header and extracting the first two (whitespace-separated) tokens, which are date and time, line after line. The dir options are set so that only files are listed (no directories) and sorted in descending order with respect to the modification date/time.

两个外部如果语句确保摘要行 DIR 输出被忽略(适应搜索也就是说,如果相应的非英语系统上工作)。

The two outer if statements make sure that the summary lines of the dir output are ignored (adapt the search words accordingly if working on a non-English system).

如果语句比较与previous之一,存储在变量中的当前解析的行的日期部分 preVIOUS 。它的值存储的之后的用于下一个循环迭代的比较。

The inner if statement compares the date portion of the current parsed line with the previous one, which is stored in variable PREVIOUS. Its value is stored after the comparison to be used for the next loop iteration.

所以这个批处理脚本的最终输出 - 在上面的例子喂 - 将​​是:

So the final output of this batch script - when fed with the example above - will be:

2015/07/31  00:30
2015/07/30  20:15
2015/07/28  17:10

要输出写入到一个文本文件,调用批处理文件(姑且称之为 daytimes.bat )以重定向:

To write the output into a text file, call the batch file (let's call it daytimes.bat) with redirection:

daytimes.bat > YourTextFile.txt

这篇关于批处理文件来检查文件的修改日期,然后把它比作另一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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