Simple rename bat

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
zip77
Posts: 3
Joined: 11 May 2012 11:10

Simple rename bat

#1 Post by zip77 » 11 May 2012 11:16

I'd like to create a bat file which renames files with extensions like: ".A30", ".A31", ".A20", etc... to ".DGN", but the catch is, I'd like the previous extension to be included in the new name.

Old Example: 05273-064.A30
New Example: 05273-064A30.DGN

I've run across some rather complex ways to do this, but I was hoping to find something simple which would do this. Any help is greatly appreciated.

Cheers

aGerman
Expert
Posts: 4740
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simple rename bat

#2 Post by aGerman » 11 May 2012 11:24

Tell some more about the possible extensions. Is it always an A with two digits or are you able to define a rule how to find the files?

Regards
aGerman

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

Re: Simple rename bat

#3 Post by foxidrive » 11 May 2012 11:29

Also, will the filenames ever contain a period apart from in the extension?

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

Re: Simple rename bat

#4 Post by foxidrive » 11 May 2012 11:31

zip77 wrote:Old Example: 05273-064.A30
New Example: 05273-064A30.DGN



The code is much simpler if you can accept the old extension like this:

New Example: 05273-064.A30.DGN

zip77
Posts: 3
Joined: 11 May 2012 11:10

Re: Simple rename bat

#5 Post by zip77 » 11 May 2012 13:42

Thanks for the responses everyone. The extension will always be 3 letters/numbers but unfortunately there is no repeating sequence to it (some start with A, F, etc..)

Yes, I would absolutely be fine with the example you proposed (foxidrive):

05273-064.A30.DGN

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Simple rename bat

#6 Post by Aacini » 11 May 2012 13:48

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for %%f in (*.*) do (
   set ext=%%~Xf
   ren "%%f" "%%~Nf!ext:~1!.DGN"
)

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

Re: Simple rename bat

#7 Post by foxidrive » 11 May 2012 14:01

This is a bug when using *.* with a basic for loop with some filenames being renamed twice or three times

EDIT: I tested Aacini's code and it didn't exhibit the bug so I wonder if using delayed expansion fixes this issue.
EDIT2: After some further simple testing I can't reproduce that bug and so perhaps it was limited to FAT32 or Win9x when it certainly was a problem.



This code below uses the simpler filename format "New Example: 05273-064.A30.DGN"

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /b /a-d') do ren "%%a" "%%a.DGN"
Last edited by foxidrive on 11 May 2012 14:13, edited 1 time in total.

zip77
Posts: 3
Joined: 11 May 2012 11:10

Re: Simple rename bat

#8 Post by zip77 » 11 May 2012 14:11

Thank you both. The only problem I found with the original was if I ran it from a bat file in the same folder it would rename it's extension too. Not a big deal though - I'm just happy to have this!

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Simple rename bat

#9 Post by Aacini » 11 May 2012 14:16

Problem solved!

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for %%f in (*.*) do (
   set ext=%%~Xf
   if /I "!ext!" neq ".bat" (
      ren "%%f" "%%~Nf!ext:~1!.DGN"
   )
)

aGerman
Expert
Posts: 4740
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simple rename bat

#10 Post by aGerman » 11 May 2012 14:19

Try:

Code: Select all

@echo off &setlocal
for /f "delims=" %%i in ('dir /a-d /b *.*^|findstr /vie "\.bat \.dgn"') do (
  set "name=%%i" &set "base=%%~ni" &set "ext=%%~xi"
  setlocal EnableDelayedExpansion
    ECHO ren "!name!" "!base!!ext:~1!.DGN"
  endlocal
)

PAUSE

Remove ECHO and PAUSE if you think it would do the right things.

Regards
aGerman

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Simple rename bat

#11 Post by Fawers » 11 May 2012 14:42

Aacini wrote:Problem solved!

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for %%f in (*.*) do (
   set ext=%%~Xf
   if /I "!ext!" neq ".bat" (
      ren "%%f" "%%~Nf!ext:~1!.DGN"
   )
)

May I suggest a simple change?
Use if /i "%%~Ff" NEQ "%~f0" ( instead of if /I "!ext!" neq ".bat" (; this way one can have .bat files renamed, as well as no problems with the running script.

Post Reply