获取最近文件的最后修改日期(递归)并将日期添加到文件夹名称

get last modification date of most recent file (recursive) and add the date to the folder name

我有一个包含子文件夹和多个文件的文件夹。

并且批处理文件在 rootdir 中

我需要获取所有文件夹中最新文件的最后修改日期 (yyyymmdd)。 获取日期后,我需要将日期添加到主文件夹名称中,例如: "Mainfolder (yyyymmdd)"

之后我需要为下一个主文件夹使用相同的文件。基本上我会得到这样的结果:

Root\Mainfolder1 (yyyymmdd)
Root\Mainfolder2 (yyyymmdd)
Root\Mainfolder3 (yyyymmdd)

附加信息:我的系统有另一种日期格式:dd.mm.yyyy(但我需要上述格式)

我希望这是一个批处理文件,我可以将其放在根目录中,然后执行它以执行我上面解释的操作。 到目前为止我有什么代码?几乎什么都没有。没有什么用。我唯一发现的是如何列出所有修改日期(但它们没有排序或任何东西):

dir /s /O:D /T:W /A:-D

而且我无法通过搜索功能找到任何东西 非常欢迎任何建议,谢谢:)

试一试。我在代码中添加了一些注释,以帮助您了解发生了什么。当您对输出满意时,删除 RENAME 命令前面的 ECHO

@echo off
setlocal enabledelayedexpansion

REM List the directories first
FOR /F "delims=" %%G IN ('dir /ad /b') DO (
    rem Change to the directory
    pushd "%%G"
    set "dt="
    set "newest="
    REM Get the date and time from the newest file from the folder.
    FOR /F "delims=" %%H IN ('dir /S /a-d /b 2^>nul') DO (
        REM by default the ~t modifier uses modified date and time
        SET "dt=%%~tH"
        REM Now split up the date into a usable format.
        REM Change the delims option to the separator your date format uses.
        REM also need a space because the time follows the date 
        FOR /F "tokens=1-3 delims=/. " %%I IN ("!dt!") DO (
            set "dt=%%K%%J%%I"
        )
        IF NOT DEFINED newest IF defined dt set "newest=!dt!" 
        IF DEFINED newest IF defined dt IF !dt! GTR !newest! set "newest=!dt!" 
    )
    POPD
    IF DEFINED newest echo rename "%%~G" "%%G(!newest!)"
)
@echo off
setlocal EnableDelayedExpansion

rem Process all folders in current dir
for /F "delims=" %%a in ('dir /B /A:D') do (

   rem Get the dates of all files in this folder and keep the most recent one
   cd "%%a"
   set "recent=0"
   for /F "tokens=1-3 delims=/. " %%d in ('dir /S /T:W /A:-D ^| findstr "^..\."') do (
      if %%f%%e%%d gtr !recent! set "recent=%%f%%e%%d"
   )

   rem Rename this folder
   cd ..
   ECHO ren "%%a" "%%a (!recent!)"

)

如果输出看起来正确,请删除最后一行中的 ECHO 部分。

这是一个批处理文件,用于确定整个给定目录树的最后修改日期;这取决于本地日期格式设置,不幸的是:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_ROOT=%~1" & rem // (take the first command line argument as the root dir.)
rem // Define sub-string portions of the local date format -- see `date /T`:
set "_Y=0,4" & rem // (year: zero-based start char. index, length in number of chars.)
set "_M=5,2" & rem // (month:zero-based start char. index, length in number of chars.)
set "_D=8,2" & rem // (day:  zero-based start char. index, length in number of chars.)

rem // Ensure `_ROOT` not to be empty:
if not defined _ROOT set "_ROOT=."

set "LAST=00000000"
for /F "delims= eol=|" %%D in ('echo "%_ROOT%"^& dir /B /S /A:D "%_ROOT%" 2^> nul') do (
    call :GETLAST FILE "%%~D" && (
        call :GETDATE FILE "%%FILE%%"
        setlocal EnableDelayedExpansion
        if !LAST! LSS !FILE! (
            endlocal
            call set "LAST=%%FILE%%"
        ) else (
            endlocal
        )
    )
)

rem // Return resulting last modification date within entire directory tree:
echo(%LAST%

endlocal
exit /B


:GETLAST  rtn_path  val_dir
    ::Get last modified file in the given directory.
    ::PARAMETERS:
    ::  rtn_path    variable to receive path to last modified file;
    ::  val_dir     directory path to search last modified file in;
    ::ERRORLEVEL:   1 if no file has been found, and 0 otherwise;
    setlocal DisableDelayedExpansion
    set "#RTN=%~1"
    set "ITEM=%~2"
    (for /F "delims= eol=|" %%F in ('dir /B /A:-D /T:W /O:-D "%ITEM%\*.*" 2^> nul') do (
        set "ITEM=%ITEM%\%%F"
        (call ) & goto :QUIT
    )) || set "ITEM="
    :QUIT
    endlocal & set "%#RTN%=%ITEM%"
    exit /B


:GETDATE  rtn_date  val_path
    ::Get last modification date of file/dir., properly formatted like `YYYYMMDD`.
    ::PARAMETERS:
    ::  rtn_date    variable to receive formatted date;
    ::  val_path    path to get last modification date of;
    setlocal DisableDelayedExpansion
    set "#RTN=%~1"
    set "ITEM=%~t2"
    setlocal EnableDelayedExpansion
    set "ITEM=!ITEM:~%_Y%!!ITEM:~%_M%!!ITEM:~%_D%!"
    endlocal & endlocal & set "%#RTN%=%ITEM%"
    exit /B