renaming extensions on files with underscores

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mayoko
Posts: 2
Joined: 05 May 2013 13:46

renaming extensions on files with underscores

#1 Post by mayoko » 05 May 2013 13:53

Hi, normally I never run into issues like this, but recently I came across some file extensions that I want to rename that include underscores, this is really irritating as the usual command I use in my batch files doesn't even print an error.

Example File:

AS_Barrel01.AG

I want to rename it to:

AS_Barrel01.back

I want this to be done to the entire directory and sub-directories if they exist.

Here's what I usually use: cd /d "C:\Folder" for /f "tokens=* delims= " %%a in ('dir /b /ad /s') do rename "%%a\*.AG" "*.back"
As I mentioned this is completely ineffective with these particular files. I tried variants like *_*.AG but it didn't work, I guess it isn't that simple. :(

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: renaming extensions on files with underscores

#2 Post by Endoro » 05 May 2013 14:42

this works here:

Code: Select all

ren *_*.ag *.back

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: renaming extensions on files with underscores

#3 Post by abc0502 » 05 May 2013 15:22

Your code is right but you made a big mistake, you told the DIR command to process Directories not Files
for /f "tokens=* delims= " %%a in ('dir /b /ad /s') do rename "%%a\*.AG" "*.back"


But For More Simple Code & As Endoro Said, try this

Code: Select all

@Echo OFF
For /F "delims=" %%A In (' DIR /B /S /A:-D "*_*.ag" ') Do REN "%%A" "%%~nA.back"
pause
This will just rename files that has "_" underscore sign and is a .ag files

Put This Batch in The Main Folder

mayoko
Posts: 2
Joined: 05 May 2013 13:46

Re: renaming extensions on files with underscores

#4 Post by mayoko » 05 May 2013 15:43

Thanks guys, I'm a little better off than a novice with command prompt, I didn't really understand the flags for that command as I couldn't look it up, it was one of the few cases I just copy and pasted from the internet, lol. :oops:

Post Reply