这段代码将适用于匹配的文件* - #.*
,其中#
是一个数字,并且*
可能包含多个(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"