Windows资源管理器的显示顺序正确。例如:filename1.txt,filename2.txt,filename11.txt等。当我尝试使用不同选项的DIR命令时,无法获得相同的行为。它显示filename1.txt,filename11.txt,filename2.txt。
有没有办法对它进行排序,以便保留数字排序?
答案 0 :(得分:1)
@ECHO Off
:: dir list in numeric-value order
SETLOCAL
:: %1= directoryname; default current directory
SET "targetdir=%~1"
IF NOT DEFINED targetdir SET "targetdir=.\*.*"
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
SET /a maxnumlength=0
SET "manyzeroes=0000000000000000"
SET "manyzeroes=%manyzeroes%%manyzeroes%%manyzeroes%%manyzeroes%"
:: analyse list of filenames
FOR /f "delims=" %%a IN (
'dir /b /a-d "%targetdir%" '
) DO CALL :detnlen "%%a"
:: build list of filenames
FOR /f "delims=" %%a IN (
'dir /b /a-d "%targetdir%" '
) DO CALL :setname "%%a"
FOR /F "tokens=1*delims=:" %%a In ('set $ 2^>Nul') DO ECHO %%b
GOTO :EOF
:detnlen
SET "name=%~1"
SET /a numlength=0
SET "action=L"
:detnloop
IF %action%==F SET "action=B"
SET "post=%name:~0,1%"&SET "name=%name:~1%"
IF "%post%" geq "0" IF "%post%" leq "9" (
SET /a numlength+=1
SET "action=F"
)
IF %action% neq B IF DEFINED name GOTO detnloop
IF %numlength% gtr %maxnumlength% SET /a maxnumlength=%numlength%
GOTO :EOF
:setname
SET "name=%~1"
SET "pre="
SET "nums="
SET "action=L"
:snloop
IF %action%==F SET "action=B"
SET "post=%name:~0,1%"&SET "name=%name:~1%"
IF "%post%" geq "0" IF "%post%" leq "9" (
SET /a numlength+=1
SET "action=F"
SET "nums=%nums%%post%"
)
IF %action%==L SET "pre=%pre%%post%"&SET "post="
IF %action% neq B IF DEFINED name GOTO snloop ELSE SET "post="
IF %action%==F SET "post="
IF DEFINED nums SET "nums=%manyzeroes%%nums%"
IF DEFINED nums CALL SET "nums=%%nums:~-%maxnumlength%%%"
SET "$%pre%%nums%%post%%name%=:%~1"
GOTO :EOF
哦,我的!
没有内置开关以奇怪的方式显示目录,但这具有挑战的所有标志。
好的 - 它太疯狂了,但似乎有效。
原则是将最长数字序列确定为maxnumlength
,然后将检测到的文件的名称分配给以$
开头的环境变量,并使用带有数字的文件名构造名称部分领先零填充。 set $
然后按字母顺序列出$
个变量;选择值,即原始文件名。