このコードは、 に一致するファイルで* - #.*
機能します。#
*
(space)-(space)
(space)-(space)(number).
SETLOCAL ENABLEDELAYEDEXPANSION
REM Call process for each file that match "name - number.extension"
FOR %%f IN ("* - *.*") DO CALL :process "" "%%f"
GOTO :EOF
REM Recursive function process: accepts two arguments: prefix and filepart
REM process checks if number part of supplied filename is a number.
REM prefix is string already checked but mismatched number. Example:
REM Call 1: "" "a - b - c - 1.ext" mismatch ("b - c - 1" is not a number)
REM Call 2: "a -" " b - c - 1.ext" mismatch ("c - 1" is not a number)
REM Call 3: "a - b -" " c - 1.ext" match ("1" is a number)
:process
REM Break filepart at first "-"
FOR /F "TOKENS=1,* DELIMS=-" %%i IN (%2) DO (
REM If after "-" is empty, file mismatch (something like "abc.ext-")
IF "%%j" == "" GOTO :EOF
REM Break part after "-" at first "."
FOR /F "TOKENS=1,* DELIMS=." %%n IN ("%%j") DO (
REM Multiply part before "." by 1. If part is not number, will evaluate to 0.
SET /A "number=1*%%n"
REM Check if rebuilt part afte "-" match using parsed number
IF "%%j" == " !number!.%%o" (
REM Matched, so do whatever is needed
ECHO Moving "%~1%~2" to "%~1%%i"...
IF NOT EXIST "%~1%%i" MD "%~1%%i"
REM MOVE "%~1%~2" "%~1%%i"
REM Exit function
GOTO :EOF
)
)
REM File part didn't match, so recall process shifting part before "-" to prefix
REM and using part after "-" as filepart.
CALL :process "%~1%%i-" "%%j"
)
たとえば、の代わりにname1 - 1. name2 - 2.txt
フォルダを作成すると、リークする可能性があります。name1
name1 - 1. name2
編集:コメントを追加しました。誤字脱字修正CALL :process "%~1%%i-" "%%j"