创建一个组合文本文件,显示每个单独文本文件的修改日期

Create a combined text file showing the date modified of each individual text file

我在一个文件夹中有几百个文本文件。我想把所有的文件合并成一个文件,archive1.txt:

代码片段:

findstr "^" *.txt >> archive1.txt

仅添加名称。

我发现了这个:

forfiles /M *.txt /C "cmd /c echo @fdate @ftime"

这似乎为每个文件找到了 'Date Modified',但我似乎无法弄清楚如何将两者结合起来并创建一个包含文件名和修改日期的文件。

成功的关键是 ~t-modifier of for meta-variables:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_ROOT=D:\Path\To\Root\Dir" & rem // (path to root dir.; use `%~dp0.` for batch file parent)
set "_MASK=*.txt"               & rem // (pattern of files to combine)
set "_FILE=archive1.txt"        & rem // (full name of the target file)

rem // Change into root directory:
pushd "%_ROOT%" && (
    rem // Write to target file:
    > "%_FILE%" (
        rem // Loop through all matching files except the target file:
        for /F "delims= eol=|" %%J in ('dir /B /A:-D-H-S "%_MASK%" ^| findstr /V /X /I /C:"%_FILE%"') do (
            rem // Store currently processed file:
            set "NAME=%%J"
            rem // Read current file with lines preceded by line numbers + `:` to maintain empty lines:
            for /F "delims=" %%I in ('findstr /N "^" "%%J"') do (
                rem // Store currently read line:
                set "LINE=%%I"
                rem // Toggle delayed expansion to avoid troubles with `!` and `^`:
                setlocal EnableDelayedExpansion
                rem // Remove line number prefix and write line with file name and date/time prefix:
                echo(!NAME!:%%~tJ:!LINE:*:=!
                endlocal
            )
        )
    )
    rem // Return from root directory:
    popd
)

endlocal
exit /B