Rename Command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
modulus
Posts: 5
Joined: 17 Jun 2013 10:42

Rename Command

#1 Post by modulus » 24 Jul 2013 10:29

This should be easy, but I'm getting stuck somewhere. I'm trying to rename files named VIDEO01234.o3g to IMAG01234.o3g. When I run the command below, VIDEO01234.o3g is being renamed to IMAGO01234.o3g (the "O" still appears). What am I doing wrong?

Code: Select all

rename VIDEO* IMAG*

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Rename Command

#2 Post by dbenham » 24 Jul 2013 14:06

You cannot do what you want with a simple RENAME command.

RENAME only allows you to substitute characters in the beginning or middle of a name component. You cannot add to/delete from the beginning or middle of a name component. You can only add or delete characters from the end of a component.

The best you can do with RENAME is to replace the O with another character, perhaps E or _.

For example, to get IMAG_01234.o3g

Code: Select all

rename VIDEO* IMAG_*

If the _ character may already appear later in the name, then you must use a series of ????? instead of *. The number of ? must be enough to reach the appearance of the first . in the existing name.

Code: Select all

rename VIDEO* IMAG_??????????????.*

See my StackExchange superUser or DosTips post for a complete explanation of how the RENAME command handles wildcards. I actively maintain the StackExchange post. The DosTips post may be out of date.

You will have to resort to a batch script if you want to drop the O from the name.

Code: Select all

@echo off
setlocal enableDelayedExpansion
for %%F in (VIDEO*.o3g) do (
  set "nm=%%F"
  rename "%%F" "IMAG!nm:~5!"
)

You need a bit more code if a name might contain the ! character

Code: Select all

@echo off
setlocal disableDelayedExpansion
for %%F in (VIDEO*.o3g) do (
  set "nm=%%F"
  setlocal enableDelayedExpansion
  rename "!nm!" "IMAG!nm:~5!"
  endlocal
)


Dave Benham

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Rename Command

#3 Post by penpen » 24 Jul 2013 16:08

If the file names and renamed file names don't contain any SPACE, then you could use this:

Code: Select all

@rename video* " imag*" & for %a in (" imag*") do @rename "%~a" %~a

penpen

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: Rename Command

#4 Post by brinda » 25 Jul 2013 06:36

dave, penpen,

thanks the examples and explanation helps a lot :)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Rename Command

#5 Post by dbenham » 25 Jul 2013 06:48

@penpen - ooh, clever. Your strategy can be made to work even with leading spaces.

Code: Select all

rename video* " imag*" & for %a in (" imag*") do @for /f "tokens=* delims= " %b in ("%~a") do @rename "%~a" "%~b"

This all assumes that you don't already have file names that have a leading space. :wink: (I sure hope you don't)

Dave Benham

Edited to include the critical TOKENS=*
Last edited by dbenham on 25 Jul 2013 11:05, edited 3 times in total.

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

Re: Rename Command

#6 Post by foxidrive » 25 Jul 2013 08:52

This also preserves spaces and some poison characters - just removes the leading spaces.

Code: Select all

rename video* " imag*" & for %a in (" imag*") do for /f "tokens=*" %b in ("%~nxa") do @rename "%~a" "%~b"

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Rename Command

#7 Post by dbenham » 25 Jul 2013 11:10

@foxidrive - I edited my post to include TOKENS=* as was originally intended.

I like your addition of %~nxa. It is not necessary in the OP's example, but would be important if the files being renamed are not in the current folder.


Dave Benham

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Rename Command

#8 Post by penpen » 25 Jul 2013 12:32

Putting all together and shortening :D then results in:

Code: Select all

rename "path\Video*" " imag*" & for /f "tokens=* delims= " %a in ('dir "path\ imag*" /b') do rename "path\ %~nxa" "%~nxa"

penpen

Post Reply