[solved] Rename list of files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
didley
Posts: 2
Joined: 24 Oct 2006 03:05

[solved] Rename list of files

#1 Post by didley » 24 Oct 2006 03:15

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?

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 25 Oct 2006 22:55

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:

didley
Posts: 2
Joined: 24 Oct 2006 03:05

#3 Post by didley » 31 Oct 2006 10:57

Hi DosItHelp,

all works fine.

thank you very mach

kaputko
Posts: 1
Joined: 14 Sep 2007 12:47

#4 Post by kaputko » 14 Sep 2007 13:12

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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#5 Post by DosItHelp » 19 Sep 2007 20:44

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:

Post Reply