How to find the last directory created in batch [d

2019-06-22 10:12发布

问题:

This question already has an answer here:

  • Get last created directory batch command 2 answers

This is my first question and I'm not very experienced using batch files so hope someone can help.

I want to find the last directory created using a batch file and have tried:

FOR /f "tokens=*" %%A in ('dir "%latestdirectory%" /AD-h /B /o-d') do (set recent=%%A)

but this result keeps returning the oldest directory not the most recent one.

Still trying to pick this up in batch.

回答1:

FOR /f "delims=" %%A in ('dir "%latestdirectory%" /AD-h /B /od') do (set recent=%%A)

for help enter dir /? at the command line.



回答2:

To get the last created subdirectory (and not the last modified one if any file or sub-sub-directory added in it), this should work:

FOR /F %%i IN ('dir /a:d /t:c /o-d /b') DO (
    SET a=%%i
    GOTO :found_last
)

echo No subfolder found
goto :eof

:found_last
echo Most recent subfolder: %a%
set last_subforlder=%a%