以下代码列出了按日期排序的目录中的所有文件夹,并在%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]%,但它没有用。
答案 0 :(得分: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
尚未实施。
请使用有意义的变量名称。
答案 1 :(得分:1)
这是另一种方法,向您展示如何使用计数器动态填充数组。
例如,您可以动态填充两个带有名称和完整路径的数组,同时我们将计数器增加到一个循环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