首页 > 代码库 > DIR 按文件名中数字大小进行排序

DIR 按文件名中数字大小进行排序

@echo off
set arg=%1
if "%arg%" == "" set arg=*
if "%arg%" == "-h" (
  echo sort by numbers in filename 
  echo usage: nsort [drive:][path][filename]
  exit /b
)

setlocal EnableExtensions
for /F "delims=" %%I in (‘dir %arg% /A-D /B /ON 2^>nul‘) do call :AddToFileList "%%I"
for /F "tokens=1* delims==" %%I in (‘set FileList[ 2^>nul‘) do echo %%J
endlocal
exit /B

:AddToFileList
set "FileName=%~n1"
if "%FileName%" == "" set "FileName=%~x1"
set "FileNumber="

:GetFileNumber
for /F "delims=0123456789" %%I in ("%FileName:~-1%") do goto AddFileToArray
set "FileNumber=%FileName:~-1%%FileNumber%"
set "FileName=%FileName:~0,-1%"
if not "%FileName%" == "" goto GetFileNumber

:AddFileToArray
set "FileNumber=00000%FileNumber%"
set "FileNumber=%FileNumber:~-5%"
set "FileList[%FileName%_%FileNumber%]=%~1"
goto :EOF
nsort.7z

DIR 按文件名中数字大小进行排序