按文件名中的两个字符串对文件排序:批处理文件

时间:2017-01-11 18:16:35

标签: sorting batch-file cmd

我想使用批处理文件按特定顺序对一组文件进行排序。 文件应按Floor编号分组,并按文件名中包含的字符串进行排序。

某些文件名的示例如下:

Floor_1_SomeName_PCI_Some Trailing Text.JPG
Floor_1_SomeName_RSRP_Some Trailing Text.JPG
Floor_1_SomeName_RSRQ_Some Trailing Text.JPG
Floor_1_SomeName_RSSI_Some Trailing Text.JPG
Floor_1_SomeName_SINR_Some Trailing Text.JPG
Floor_1_SomeName_TX Power_Some Trailing Text.JPG

Floor_2_SomeName_PCI_Some Trailing Text.JPG
Floor_2_SomeName_RSRP_Some Trailing Text.JPG
Floor_2_SomeName_RSRQ_Some Trailing Text.JPG
Floor_2_SomeName_RSSI_Some Trailing Text.JPG
Floor_2_SomeName_SINR_Some Trailing Text.JPG
Floor_2_SomeName_TX Power_Some Trailing Text.JPG

我想首先按Floor号码排序所有文件,然后按以下顺序排序:

RSSI
RSRP
RSRQ
SINR
TX Power
PCI

有没有办法可以利用sort命令来组织这个?

3 个答案:

答案 0 :(得分:3)

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Retrieve folder from command line. By default current folder
    for %%a in ("%~f1.") do set "folder=%%~fa"

    rem Configure the order of the files
    set /a "RSSI=1", "RSRP=2", "RSRQ=3", "SINR=4", "Tx=5", "PCI=6"

    rem %%f - For each file in the indicated folder
    rem     %%a %%b %%c - Separate the elements of the file name
    rem     Use the elements to generate a serial number for the file
    rem     Output the serial number and the file name
    rem Pipe the generated list into sort to sort on serial number
    rem %%d Retrieve the sorted list 
    rem     separate serial number and file name
    rem     output file name

    (
        for %%f in ("%folder%\Floor*") do @(
            for /f "tokens=2,4 delims=_" %%a in ("%%~nxf") do @for /f %%c in ("%%b") do @(
                set /a 10000000+%%a*100000+%%c
                echo( %%~nxf
            )
        )
    ) | sort | for /f "tokens=1,*" %%d in ('findstr "^"') do @echo(%%e

输出

W:\41597911>dir /B *.jpg
Floor_1_SomeName_PCI_Some Trailing Text.JPG
Floor_1_SomeName_RSRP_Some Trailing Text.JPG
Floor_1_SomeName_RSRQ_Some Trailing Text.JPG
Floor_1_SomeName_RSSI_Some Trailing Text.JPG
Floor_1_SomeName_SINR_Some Trailing Text.JPG
Floor_1_SomeName_TX Power_Some Trailing Text.JPG
Floor_2_SomeName_PCI_Some Trailing Text.JPG
Floor_2_SomeName_RSRP_Some Trailing Text.JPG
Floor_2_SomeName_RSRQ_Some Trailing Text.JPG
Floor_2_SomeName_RSSI_Some Trailing Text.JPG
Floor_2_SomeName_SINR_Some Trailing Text.JPG
Floor_2_SomeName_TX Power_Some Trailing Text.JPG

W:\41597911>sortFiles.cmd
Floor_1_SomeName_RSSI_Some Trailing Text.JPG
Floor_1_SomeName_RSRP_Some Trailing Text.JPG
Floor_1_SomeName_RSRQ_Some Trailing Text.JPG
Floor_1_SomeName_SINR_Some Trailing Text.JPG
Floor_1_SomeName_TX Power_Some Trailing Text.JPG
Floor_1_SomeName_PCI_Some Trailing Text.JPG
Floor_2_SomeName_RSSI_Some Trailing Text.JPG
Floor_2_SomeName_RSRP_Some Trailing Text.JPG
Floor_2_SomeName_RSRQ_Some Trailing Text.JPG
Floor_2_SomeName_SINR_Some Trailing Text.JPG
Floor_2_SomeName_TX Power_Some Trailing Text.JPG
Floor_2_SomeName_PCI_Some Trailing Text.JPG        

答案 1 :(得分:2)

当OP没有发布他/她自己的努力时,我不习惯发布代码。但是,这个问题对我来说很有意思,所以这里有一个解决方案:

Floor_1_SomeName_RSSI_Some Trailing Text.JPG
Floor_1_SomeName_RSRP_Some Trailing Text.JPG
Floor_1_SomeName_RSRQ_Some Trailing Text.JPG
Floor_1_SomeName_SINR_Some Trailing Text.JPG
Floor_1_SomeName_TX Power_Some Trailing Text.JPG
Floor_1_SomeName_PCI_Some Trailing Text.JPG
Floor_2_SomeName_RSSI_Some Trailing Text.JPG
Floor_2_SomeName_RSRP_Some Trailing Text.JPG
Floor_2_SomeName_RSRQ_Some Trailing Text.JPG
Floor_2_SomeName_SINR_Some Trailing Text.JPG
Floor_2_SomeName_TX Power_Some Trailing Text.JPG
Floor_2_SomeName_PCI_Some Trailing Text.JPG

输出:

margin:0 auto

您可以在this answer的批处理文件中查看阵列管理。

答案 2 :(得分:0)

这是另一种使用临时文件的方法;这包含一个匹配文件名列表,可以多次读取,每个关键字<head> <script language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> </head> RSSIRSRPRSRQSINR和{{ 1}}(从文件中读取比在性能方面构建文件列表更好)。这是代码:

TX Power

依赖于您的示例数据,上述临时文件包含以下列表:

PCI

对于由@echo off setlocal EnableExtensions DisableDelayedExpansion rem // Define constants here: set "_FILES=.\Floor_*_*_*_*.jpg" & rem // (location and pattern of files to be sorted) rem // Create temporary file containing list of all matching file names: > "%~dpn0.tmp" ( rem // Capture filtered list of files and split them into `_`-separated tokens: for /F "tokens=1-4* delims=_" %%A in (' dir /B /A:-D /O:N "%_FILES%" ^| ^( rem/ Filter for files whose names match the given pattern: ^& ^ findstr /R /I /C:"^Floor_[0-9][0-9]*_[^_][^_]*_[A-Z ][A-Z ]*_[^_].*\.jpg$" ^) ') do ( rem // Pad floor number with leading zeroes: set "NUM=0000%%B" rem /* Store floor number prefix in variable named `$FLOOR_`, followed by rem four-digit floor number, to get a list of unique floor numbers: */ call set "$FLOOR_%%NUM:~-4%%=%%A_%%B" rem // Write original file name into temporary file: echo(%%A_%%B_%%C_%%D_%%E ) ) rem /* Loop over the unique floor numbers by retrieving a sorted list of all variables rem whose names begin with `$FLOOR_`; the following zero-padded four-digit floor rem number ensures ascending numerical sort order (sorted by `set` implicitly): */ for /F "tokens=1,* delims==" %%E in ('2^> nul set $FLOOR_') do ( rem /* Return file names from temporary file holding the currently iterated floor rem number with the special keywords (fourth token) in the proposed order: */ findstr /R /I /C:"^%%F_[^_][^_]*_RSSI_" "%~dpn0.tmp" findstr /R /I /C:"^%%F_[^_][^_]*_RSRP_" "%~dpn0.tmp" findstr /R /I /C:"^%%F_[^_][^_]*_RSRQ_" "%~dpn0.tmp" findstr /R /I /C:"^%%F_[^_][^_]*_SINR_" "%~dpn0.tmp" findstr /R /I /C:"^%%F_[^_][^_]*_TX Power_" "%~dpn0.tmp" findstr /R /I /C:"^%%F_[^_][^_]*_PCI_" "%~dpn0.tmp" ) rem // Delete temporary file: del "%~dpn0.tmp" endlocal exit /B 和数字组成的每个唯一的楼层前缀,在建议的订单中一个接一个地搜索上面的列表以寻找预定义的关键字。为了获得所有唯一的地板前缀,使用名为Floor_1_SomeName_PCI_Some Trailing Text.JPG Floor_1_SomeName_RSRP_Some Trailing Text.JPG Floor_1_SomeName_RSRQ_Some Trailing Text.JPG Floor_1_SomeName_RSSI_Some Trailing Text.JPG Floor_1_SomeName_SINR_Some Trailing Text.JPG Floor_1_SomeName_TX Power_Some Trailing Text.JPG Floor_2_SomeName_PCI_Some Trailing Text.JPG Floor_2_SomeName_RSRP_Some Trailing Text.JPG Floor_2_SomeName_RSRQ_Some Trailing Text.JPG Floor_2_SomeName_RSSI_Some Trailing Text.JPG Floor_2_SomeName_SINR_Some Trailing Text.JPG Floor_2_SomeName_TX Power_Some Trailing Text.JPG 的数组式变量集,后跟楼层号,其各自的值保存在文件名中出现的楼层前缀。要使这些前缀以字母数字方式排序,变量名称中的楼层编号将零填充为四位数:

Floor_

最终返回的结果将是:

$FLOOR_