Simple rename bat
Moderator: DosItHelp
Simple rename bat
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
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
Re: Simple rename bat
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
Regards
aGerman
Re: Simple rename bat
Also, will the filenames ever contain a period apart from in the extension?
Re: Simple rename bat
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
Re: Simple rename bat
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
Yes, I would absolutely be fine with the example you proposed (foxidrive):
05273-064.A30.DGN
Re: Simple rename bat
Code: Select all
@echo off
setlocal EnableDelayedExpansion
for %%f in (*.*) do (
set ext=%%~Xf
ren "%%f" "%%~Nf!ext:~1!.DGN"
)
Re: Simple rename bat
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"
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.
Re: Simple rename bat
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!
Re: Simple rename bat
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"
)
)
Re: Simple rename bat
Try:
Remove ECHO and PAUSE if you think it would do the right things.
Regards
aGerman
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
Re: Simple rename bat
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.