好吧,首先/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 或者,如果您不想显示时间和日期,请将 type 行替换为:
@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
要反转排序顺序,请使用 /R 开关和sort
,如下所示:
type tmp.txt | sort /R
*目录和文件夹是一回事。