Extending file names with a rename command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cplloyd42
Posts: 2
Joined: 24 Dec 2013 02:35

Extending file names with a rename command

#1 Post by cplloyd42 » 24 Dec 2013 03:01

Hi all,

I've just joined this group because I have run into a problem:
I have a large number of files that I want to rename. The format of the current name is IMG_0???.CR2. What I want to change them to is IMG_10???.CR2, i.e. 1 character longer before the extension. For example, the file IMG_0034.CR2 would become IMG_10034.CR2. Is there a DOS command that will let me do this?
I have tried REN IMG_0*.* IMG_10*.*, and REN IMG_0???.* IMG_10???.*, but both these maintain the length of the file name before the extension (8 chars), so that the end character of the original file name is dropped. I also tried the same with the COPY command with the same results. A simple line command would be a whole lot easier than individually changing them via Windows Explorer.

At this point I have run out of options. Can anyone assist?

Thanks,
Paul.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Extending file names with a rename command

#2 Post by foxidrive » 24 Dec 2013 04:31

Save this as RenIN.bat and use it in your case like this:

Code: Select all

RenIn *.CR2 5 "1" 6



It will take 5 characters from the start of each *.CR2 filename, and add another character "1" and then continue with the original filename and extension from character 6

To undo the most recent change then execute this command

Code: Select all

RenIN !




Code: Select all

@echo off

if "%~1"=="!" (call "%temp%\renin!.bat" & goto EOF)

if not "%~4"=="" goto start
echo.
echo.  Syntax: %0 filespec.ext num1 char$ num2 [ext]
echo.  UNDO: "%~nx0" !
echo.  Takes list of filespec.ext and renames them using:
echo.
echo.  num1  - The number of characters from the start of each filename
echo.  char$ - The characters to insert next (non-alphanumerics must be "quoted")
echo.  num2  - The character position from the original filename to take as the
echo.          remainder of the new filename
echo.  ext   - optional new extension with no period
echo.
echo.  (wildcards supported - requires W2K and higher)
echo.
echo.  EG:
echo.  %0 *.JPG 0 "Cat & Dogs" 4
echo.  Then all matching filenames become "Cat & Dogs*.JPG"
echo.  where * is from character 4 of the original filenames
goto :EOF
:start



del "%temp%\renin!.bat" 2>nul

set num1=%2
for /f "delims=" %%a in ("%~3") do set "partb=%%~a"
set /a num2=%4-1
set  ext=%5

for /f "delims=" %%a in ('dir %1 /a-d /b /o:n') do call :next "%%~fa"
pause
goto :eof

:next
if defined ext (set "e=.%ext%") else (set "e=%~x1")
set "fn=%~n1"
call set "parta=%%fn:~0,%num1%%%"
call set "partc=%%fn:~%num2%%%"
echo renaming "%~nx1" to "%parta%%partb%%partc%%ext%"
ren "%~1" "%parta%%partb%%partc%%e%"
>>"%temp%\renin!.bat" echo ren "%parta%%partb%%partc%%e%" "%~nx1"

goto :EOF


cplloyd42
Posts: 2
Joined: 24 Dec 2013 02:35

Re: Extending file names with a rename command

#3 Post by cplloyd42 » 25 Dec 2013 03:26

foxidrive,

Many, many thanks for that. It's probably been more than 20 years since I have had the need to write a DOS batch script, and I know the capabilities of the language has gone well beyond my knowledge. This is by way of saying that your script is well beyond my capabilities to generate. So again, many thanks for your help. It works just as I want it to.

Cheers and best wishes from a delighted forum member,
Paul.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Extending file names with a rename command

#4 Post by foxidrive » 25 Dec 2013 04:08

I'm glad it helps Paul.

I had it here for my own use in the past - so t'was but a quick copy and paste.

Eldig_Tilf
Posts: 1
Joined: 23 Apr 2017 05:58

Re: Extending file names with a rename command

#5 Post by Eldig_Tilf » 23 Apr 2017 06:16

I just happened to find your post when searching for an identical problem as "cplloyd42" and I'm so happy I found it. Even though it's a few years old.
Just like another another guy from the history books, veni vidi vici, thanks to you.
As "cplloyd42" said, your knowledge is way above mine regarding this.

Post Reply