使用批处理打印数组中的值

Print the values in an array using batch

以下代码列出目录中的所有文件夹,按日期排序并打印 %a%

中的最新文件夹
FOR /F "delims=" %%i IN ('dir "directory" /b /ad-h /t:c /od') DO SET a=%%i
echo Most recent subfolder: %a%

如何打印最近的第二张?我尝试使用 %a[1]% 但它没有用。

set "a="
FOR /F "delims=" %%i IN ('dir "directory" /b /ad-h /t:c /o-d') DO if not defined a SET "a=%%i"

设置最新

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"

设置倒数第二

遗憾的是,skip=0 未实现。

请使用有意义的变量名。

这是向您展示如何使用计数器动态填充数组的另一种方法。

例如,当我们将计数器递增到循环 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