简体   繁体   中英

Second latest folder in a Directory

I want a batch file which will find out which is the second latest folder created/modified in a directory. I found this article but no matter how much i tried i could not understand how it works

@echo off

set "root_dir=c:\somewhere"
pushd "%root_dir%"
set "bl1="
set "bl2="
setlocal enableDelayedExpansion
for /f "tokens=* delims=" %%# in ('dir /b /a:-d /o:d') do (
set "bl2=!bl1!"
set "bl1=%%#"
)
echo %bl2%
endlocal

If i use it as it is then i can get the second latest folder but this script is supposedly able to get which ever latest folder you need , be it 1st or nth. Could someone please tell me what modifications need to be done to the script to accomplish that. Also how exactly this script works

In your approach, the latest folder is already available in variable bl1 ; add echo %bl1% at the end before endlocal to display it. Retrieving the n th folder is simply not possible in a flexible way with that script as you would need to define another variable (say bl3 , bl4 ,..., bl n ) within the loop.

However, you could reverse the sort order of the output of the dir command by changing the /O option, so it returns the latest (most recent) item first. Then let an index number count the iterations of the loop, and if that index equals the predefined number n , store the currently iterated item:

@echo off
setlocal EnableDelayedExpansion

rem // Define N here to get Nth-latest folder:
set /A LATEST=2

set /A INDEX=0
for /F "eol=| delims=" %%# in ('dir /B /A:D /O:-D "C:\somewhere"') do (
    set /A INDEX+=1
    if !INDEX! EQU !LATEST! (
        set "ITEM=%%#"
    )
)
if defined ITEM echo %LATEST%th-latest folder: %ITEM%

endlocal
exit /B

Update

Here is a modified script with the following improvements:

  • Exclamation marks ! in folder names are no longer lost due to toggling delayed expansion;
  • the target directory can be provided as the first command line argument; if omitted, the current directory is used;
  • the number n can be given as the second command line argument; if omitted, the user is prompted for it (this addresses elzooilogico 's comment ); n defaults to 1 for empty input;
  • the display output is improved to avoid something weird like 1th-latest , 2th-latest and 3th-latest ; instead, The latest , 2nd-latest and 3rd-latest is returned, respectively;

So this is the code:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem /* define location path and folder pattern as 1st command line argument;
rem /* Define number N as 2nd command line argument to get Nth-latest folder. */

set "LATEST=%~2"
set /A LATEST+=0
if %LATEST% LEQ 0 (set /P LATEST="Enter N [1]: " & set /A LATEST+=0)
if %LATEST% LEQ 0 set /A LATEST=1
set /A INDEX=0
for /F "eol=| delims=" %%# in ('dir /B /A:D /O:-D "%~1"') do (
    set /A INDEX+=1
    setlocal EnableDelayedExpansion
    if !INDEX! EQU !LATEST! (
        endlocal
        set "ITEM=%%#"
        goto :SKIP & rem // break loop after having retrieved Nth-latest folder;
    ) else endlocal
)
:SKIP
setlocal EnableDelayedExpansion
if defined ITEM (
    if %LATEST% EQU 1 (echo The latest file: !ITEM!) else (
    if %LATEST% EQU 2 (echo 2nd-latest file: !ITEM!) else (
    if %LATEST% EQU 3 (echo 3rd-latest file: !ITEM!) else (
    echo %LATEST%th-latest file: !ITEM!)))
)
endlocal

endlocal
exit /B

To achieve a similar result as with the simple script on top of this answer, you need to call this script by the following command line, supposing it has been saved as Nth-latest.bat :

Nth-latest.bat "C:\somewhere" 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM