Page 1 of 1

[solved] Rename list of files

Posted: 24 Oct 2006 03:15
by didley
Hi,

I'm looking for a solution for the problem how can I rename a list of files?
For example: ren Table_23454423.txt 2345423.txt. This works for one file. But I have a list of files like Table_XXXXX.txt and all should be renamed in XXXXX.txt.
I assume I need the command 'For' for it. But I don't know how I can use it. Is anybody there which can solve this problem?

Posted: 25 Oct 2006 22:55
by DosItHelp
didley,

Try this:
Create a batch file, i.e. myrename.bat
put the following code in it:

Code: Select all

@ECHO OFF
for %%a in (*.txt) do @set f=%%a&call ren "%%a" "%%f:~6%%"


The %%f:~6%% will cut off the first 6 characters.
For testing you can temporary replace the the 'ren' command with 'echo'.


Or from a command line run this:

Code: Select all

cmd /v:on /c "for %a in (*.txt) do @set f=%a&ren %a !f:~6!"

Now !f:~6! will cut off the first 6 characters.
Again for testing you can temporary replace the the 'ren' command with 'echo'.

DOS IT HELP ? :wink:

Posted: 31 Oct 2006 10:57
by didley
Hi DosItHelp,

all works fine.

thank you very mach

Posted: 14 Sep 2007 13:12
by kaputko
Hi,

Is there a way to rename a list of files like "random numbers_xxx_-_xxx.txt" , to "xxx xxx.txt" ?


thank you in advance







________
sorry for my bad english

Posted: 19 Sep 2007 20:44
by DosItHelp
kaputko,

Sure, here is one way this can be done:

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (*.txt) do (
    set "f=%%a"
    set "f=!f:random numbers_=!"&REM REMOVES "random numbers_"
    set "f=!f:_-_= !"&REM REPLACES "_-_" WITH A SINGLE SPACE
    ren "%%a" "!f!"
)


Here another one:

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (*.txt) do (
    set "f=%%a"
    ren "%%a" "!f:~15,3! !f:~21!"
)

works too because:
"!f:~15,3!" - extracts 3 characters beginning at position 15 (start couting at 0)
"!f:~21!" - extracts all characters after position 21 (start counting at 0)

DOS IT HELP? :wink: