Naturally Sort Files in Batch

前端 未结 1 577
栀梦
栀梦 2020-12-22 03:51

I have a folder with 20 some files and am wanting to sort them similar to the way Windows Explorer does. The files I have all have prefixes of block sizes like so:



        
相关标签:
1条回答
  • 2020-12-22 04:23

    Take Command - the CMD.EXE replacement has natural sorting.

    If you can pad the filenames with zeros then a normal sort will return them ok.

    This code will return them sorted numerically:

    @echo off
    type nul>1024KB.log
    type nul>32KB.log
    type nul>64KB.log
    type nul>4KB.log
    type nul>256KB.log
    type nul>512KB.log
    
    setlocal enabledelayedexpansion
    for %%a in (*.log) do (
    set num=0000000000%%a
    set num=!num:~-14!
    set $!num!=%%a
    )
    for /f "tokens=1,* delims==" %%a in ('set $0') do echo %%b
    pause
    
    0 讨论(0)
提交回复
热议问题