好吧,首先/a-d 和/od 不是dir 的开关。我相信你的意思分别是/a:d 和/o:d。
除了这些错误之外,您的第一个 for 语句不起作用的原因是 DIR \stDesign.cmd /B /S /a:d 正在搜索名为 stDesign.cmd 的 目录*,而不是文件.
您的第二个for 声明告诉DIR 按创建日期对文件进行排序,但这仅适用于相同目录中的文件,而不适用于跨目录。这意味着同一目录中的任何匹配文件都将按日期排序,但存在于单独目录中的文件将按DIR 找到它们的顺序排列。
试试
@echo off
echo.
REM Housekeeping: Delete any existing files left-over from previous runs
if exist "%tmp%\~.tm?" del "%tmp%\~.tm?"
REM Find the files
for %%v in (*.bat) do (
REM Extract Date, Time, Path, File Name, and Extention
for /f "tokens=1-3* delims=/ " %%w in ("%%~tdpnxv") do (
REM Change the date around so that simply sorting the lines
REM will order them by date and time.
echo %%y/%%w/%%x %%z>>%tmp%\~.tmp
)
)
REM Sort (in ascending order) and Display data
type "%tmp%\~.tmp" | sort
REM Housekeeping
del "%tmp%\~.tmp"
echo.
REM 或者,如果您不想显示时间和日期,请将类型行替换为:
@echo off
echo.
REM Housekeeping: Delete any existing files left-over from previous runs
if exist "%tmp%\~.tm?" del "%tmp%\~.tm?"
REM Find the files
for %%v in (*.bat) do (
REM Extract Date, Time, Path, File Name, and Extention
for /f "tokens=1-3* delims=/ " %%w in ("%%~tdpnxv") do (
REM Change the date around so that simply sorting the lines
REM will order them by date and time.
echo %%y/%%w/%%x %%z>>%tmp%\~.tmp
)
)
REM Sort (in ascending order) and save data to ~.tm2
type "%tmp%\~.tmp" | sort >> "%tmp%\~.tm2"
REM Extract path and file name only from ~.tm2 and display it
for /f "tokens=4* usebackq" %%x in ("%tmp%\~.tm2") do echo %%x
REM Housekeeping
del "%tmp%\~.tmp"
echo.
第一个例子给出这样的输出:
1992\01\15 00:12:55 C:\SOMEPATH\FILE0001.EXT
2004\04\17 00:42:17 C:\ANOTHERPATH\FILE0002.EXT
2004\04\17 12:42:17 C:\MISCPATH\RANDOM\FILE0003.EXT
2012\10\15 00:12:55 C:\EXAMPLE\FILE0004.EXT
2012\01\15 00:12:55 C:\FILE0005.EXT
第二个示例仅显示路径和文件名,但仍按日期+时间排序。
C:\SOMEPATH\FILE0001.EXT
C:\ANOTHERPATH\FILE0002.EXT
C:\MISCPATH\RANDOM\FILE0003.EXT
C:\EXAMPLE\FILE0004.EXT
C:\FILE0005.EXT
要反转排序顺序,请使用带有sort 的/R 开关,如下所示:
type tmp.txt | sort /R
*目录和文件夹是一回事。