Here is another approach that uses a temporary file; this holds a list of matching file names, which is read multiple times, once per keyword RSSI
, RSRP
, RSRQ
, SINR
, TX Power
and PCI
(reading from a file is better than building the list of files in terms of performance). Here is the code:
@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
Relying on your example data, the aforementioned temporary file contains the following list:
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
For every unique floor prefix consisting of Floor_
and a number, the above list is searched for the predefined keywords one after another in the proposed order. To get all the unique floor prefixes, an array-like variable set named $FLOOR_
, followed by the floor number, is used, whose respective values hold the floor prefix as they appear in the file names. For these prefixes to appear sorted in an alphanumerical manner, the floor number in the variable names are zero-padded to four digits:
$FLOOR_0001=Floor_1
$FLOOR_0002=Floor_2
The finally returned result is going to be this:
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