Renaming a group of files, at the same time, using a regex to concatenate a number before their extension - Python or Batch Script

浪子不回头ぞ 提交于 2019-12-24 10:15:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!