Page 1 of 1

Rename files

Posted: 10 Jan 2013 15:54
by aveit
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

Re: Rename files

Posted: 10 Jan 2013 16:19
by foxidrive
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

Re: Rename files

Posted: 10 Jan 2013 18:07
by abc0502
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.

Re: Rename files

Posted: 14 Jan 2013 13:06
by aveit
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

Re: Rename files

Posted: 14 Jan 2013 16:02
by foxidrive
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