Rename files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aveit
Posts: 8
Joined: 17 Oct 2011 05:16

Rename files

#1 Post by aveit » 10 Jan 2013 15:54

Hi

In a folder i have a number of files which i would like to rename. They are currently named like this:

111111_1

111111_2

111111_3

33333333_1

33333333_2

I would like them to be outputted like so:

0000111111_PF_20130110_1

0000111111_PF_20130110_2

0000111111_PF_20130110_3

0003333333_PF_20130110_1

0003333333_PF_20130110_2

The first part of the file name should be padded with 0's to 10 characters. _PF_20130110 is a fixed text which need to be added.

Is this possible?

Many thanks Paul

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

Re: Rename files

#2 Post by foxidrive » 10 Jan 2013 16:19

Untested - remove the echo to enable the ren command, otherwise it will just print the commands to the console for you to check.

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=_" %%a in ('dir *_? /b /a-d') do (
set "name=0000000000%%a"
set "name=!name:~-10!"
echo ren "%%a_%%b" "!name!_PF_20130110_%%b"
)
pause

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

Re: Rename files

#3 Post by abc0502 » 10 Jan 2013 18:07

just a note, the *_? in the for command, it should be *.* or replace the 2nd * with the file extension.
the code that way doesn't work for me at least.


Edited:
My bad :oops:
The code work fine if the files have no extension.

aveit
Posts: 8
Joined: 17 Oct 2011 05:16

Re: Rename files

#4 Post by aveit » 14 Jan 2013 13:06

Cheers guy that worked a treat.

However, some idiot (yes me!) used the naming _PF_ instead of _PS_. Is there a way to replace _PF_ with _PS_ in the file name?

Cheers
Paul

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

Re: Rename files

#5 Post by foxidrive » 14 Jan 2013 16:02

Do you mean you renamed them all and now need to change them?

If so then try this - the rename command will simply be echoed to the screen.
If it looks right then remove the echo below, and run the batch file again to actually rename them.

Code: Select all

@echo off
for %%z in (*_PF_*) do (
for /f "tokens=1,2,* delims=_" %%a in ("%%z") do (
echo ren "%%z" "%%a_PS_%%c"
)
)
pause

Post Reply