自然排序文件批处理 [英] Naturally Sort Files in Batch

查看:277
本文介绍了自然排序文件批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些20文件的文件夹和我想对它们进行排序类似于Windows资源管理器的方式。我把所有的文件有块大小的prefixes像这样:

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:

1024KB.log
32KB.log
64KB.log
4KB.log
256KB.log
512KB.log

但是,当我在批次排序,它只是在第一位看上去那么它们排序,像这样:

But when I sort them in batch, it only looks at the first digit then sorts them like so:

1024KB.log
256KB.log
32KB.log
4KB.log
512KB.log
64KB.log

我想通过最小的排序他们最大的块大小。任何想法?

I want to sort them by smallest to largest block size. Any ideas?

编辑:我也有保留的文件名,完整性,因为我然后调用它使用文件名和字符串创建另一个脚本

I also have to keep the file name integrity because I then call another script which uses the file names and creates strings.

推荐答案

以命令 - 在CMD.EXE更换具有天然的分选

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

如果你能垫零则文件名一个正常排序将返回他们ok了。

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

这code将返回他们的数字顺序排序:

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

这篇关于自然排序文件批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆