Reputation: 37
The following code lists all the folders in a directory sorted by date and prints the latest folder in %a%
FOR /F "delims=" %%i IN ('dir "directory" /b /ad-h /t:c /od') DO SET a=%%i
echo Most recent subfolder: %a%
How do i print the second recent one? I tried using %a[1]% but it did not work.
Upvotes: 1
Views: 99
Reputation: 18827
Here is another method that show you how to populate array dynamically using a counter.
You can for example populate two arrays with names and full paths dynamically while we increment the counter into a loop forindo
@echo off
Title Print the values in an array using batch
Set "MasterFolder=C:\FRST"
Set "RecentFolder="
set /a "count=0"
Setlocal EnableDelayedExpansion
Rem Populate two arrays with names and full paths dynamically while we increment the counter
@FOR /F "delims=" %%a IN ('dir "%MasterFolder%" /b /ad-h /t:c /o-d') DO (
if not defined RecentFolder (
set /a "Count+=1"
set "RecentFolderName[!Count!]=%%~na"
set "RecentFolderPath[!count!]=%%~fa"
)
)
Rem Display numbered Folders Names and full paths
color 0A & Mode 90,30 & cls & echo(
@for /L %%i in (1,1,%Count%) do (
set "RecentName=[%%i] - !RecentFolderName[%%i]!"
set "RecentFullPath=FullPath - "!RecentFolderPath[%%i]!""
echo !RecentName!
echo !RecentFullPath!
echo --------------------------------------------------
)
Pause
Upvotes: 1
Reputation: 80013
set "a="
FOR /F "delims=" %%i IN ('dir "directory" /b /ad-h /t:c /o-d') DO if not defined a SET "a=%%i"
sets the latest
set "a="
FOR /F "skip=1delims=" %%i IN ('dir "directory" /b /ad-h /t:c /o-d') DO if not defined a SET "a=%%i"
sets the second-latest
sadly, skip=0
is not implemented.
Please use meaningful variable-names.
Upvotes: 1