更新时间:2023-01-29 12:38:23
下面的返回唯一修改日期的列表,并为每一天的最晚时间,最近的日期将出现第一个:
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:\***\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
命令,用于由变量给出的文件夹文件夹
,其中输出是这样的:
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:\***\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