问题
I want to rename each file that ends in .log, but I have similar files like:
x.log
x01.log
y.log
y01.log
....
I want to rename every file, at a time, with that extension (.*) but without any number, i.e., I want to rename x.log to xN.log, y.log to yNlog, z.log to zN.log, in which N is the number I want to concatenate.
Is this possible in a Windows batch script using a regex or even Python?
Anything will do, as long as I can give the parameter N.
回答1:
SET ext=.log
SET num=%1
FOR %%f IN (*%ext%) DO CALL :process "%%~nf"
GOTO :EOF
:process
SET name=%~1
SET lastchar=%name:~-1,1%
IF "%lastchar%" GEQ "0" IF "%lastchar%" LEQ "9" GOTO :EOF
RENAME "%name%%ext%" "%name%%num%%ext%"
来源:https://stackoverflow.com/questions/5208254/renaming-a-group-of-files-at-the-same-time-using-a-regex-to-concatenate-a-numb