Page 1 of 1

Rename Command

Posted: 24 Jul 2013 10:29
by modulus
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*

Re: Rename Command

Posted: 24 Jul 2013 14:06
by dbenham
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

Re: Rename Command

Posted: 24 Jul 2013 16:08
by penpen
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

Re: Rename Command

Posted: 25 Jul 2013 06:36
by brinda
dave, penpen,

thanks the examples and explanation helps a lot :)

Re: Rename Command

Posted: 25 Jul 2013 06:48
by dbenham
@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=*

Re: Rename Command

Posted: 25 Jul 2013 08:52
by foxidrive
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"

Re: Rename Command

Posted: 25 Jul 2013 11:10
by dbenham
@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

Re: Rename Command

Posted: 25 Jul 2013 12:32
by penpen
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