在批处理文件中使用定界符对文件名进行排序 [英] Sort file name with delimiter in batch file

查看:93
本文介绍了在批处理文件中使用定界符对文件名进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows批处理脚本中,我需要按文件名列表的版本号排序,至少要最先. 文件名如下:

I need to sort the list of file names by their version number with least first, in windows batch script. The file names are like:

2_0_0to2_0_1
2_0_1_to2_0_2
...
2_0_12_to2_0_13
...

我尝试在Windows中进行dir和sort,但是似乎只查看char位置,在两位数的情况下不起作用.在Linux中,我使用以下命令完成了此操作:ls * .txt |排序-n -t _ -k1 -k2 -k3.如何在Windows上执行此操作.请帮忙.谢谢!

I've tried dir and sort in Windows but it seems to only look at char positions which wouldn't work in the case of double digits. In Linux, I've done this with: ls *.txt | sort -n -t _ -k1 -k2 -k3. How to do this on Windows. Please help. Thanks!

推荐答案

尝试一下:

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%x IN (*) DO (
    FOR /f "tokens=1-6delims=_to" %%a IN ("%%~x") DO (
        SET "g1=0%%a"
        SET "g2=0%%b"
        SET "g3=0%%c"
        SET "g4=0%%d"
        SET "g5=0%%e"
        SET "g6=0%%f"
        SET "$!g1:~-2!!g2:~-2!!g3:~-2!!g4:~-2!!g5:~-2!!g6:~-2!=%%~x"
    )
)
FOR /f "tokens=2delims==" %%a IN ('set "$"') DO ECHO %%~a

这篇关于在批处理文件中使用定界符对文件名进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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