Reputation: 739
I just need to rename sequence of files from, say ######.avi to ###two.avi using .bat So I need to remove 3 last symbols before extension and change them into "two". I found a solution for prefix changing,
@echo off
for %%i in (*.avi) do (set fname=%%i) & call :rename
goto :eof
:rename
ren %fname% two%fname:~3%
goto :eof
but nothing on suffix. Thanks in advance.
UPD. Thanx man! That's the solution. With I could head you up with +1 rep, but I can't. (don't have the 15 rep required) Also, to not write so much, this can be done in shorter text:
@echo off
for %%i in (*.avi) do (set fname=%%i) & call :rename
:renameFile
ren %fname% %fname:~0,3%two.avi
Upvotes: 0
Views: 840
Reputation: 130819
You were close, you just need the ~n expansion modifier to get the base name without the extension. It works with both FOR variables as well as CALL parameters. I would change the name of your :rename routine so that it cannot be confused with the RENAME command).
@echo off
for %%i in (*.avi) do (set fname=%%~ni) & call :renameFile
goto :eof
:renameFile
ren "%fname%.avi" "%fname:~0,-3%two.avi"
goto :eof
If you activate delayed expansion, you don't need the subroutine. I toggle delayed expansion within the loop so that a file name with !
is not corrupted during the assignment of fname.
@echo off
for %%i in (*.avi) do (
set fname=%%~ni
setlocal enableDelayedExpansion
ren "!fname!.avi" "!fname:~0,-3!two.avi"
endlocal
)
Upvotes: 2