CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Renaming whole group of files with fix naming format

 



meetvivek72
New User

Jun 28, 2013, 1:57 AM

Post #1 of 3 (318 views)
Renaming whole group of files with fix naming format Can't Post

we have many folders which contains 96 files like:
A01 to A12, B1 to B12,C1 to C12,D1 to D12,E1 to E12, F1 to F12,G1 to G12,H1 to H12.. We also have shown the A series files of folder number 1635....
A01_Wgenome0011P3_2013-05-18_015
A02_Wgenome0011P3_2013-05-18_016
A03_Wgenome0011P3_2013-05-18_031
A04_Wgenome0011P3_2013-05-18_032
A05_Wgenome0011P3_2013-05-18_047
A06_Wgenome0011P3_2013-05-18_048
A07_Wgenome0011P3_2013-05-18_063
A08_Wgenome0011P3_2013-05-18_064
A09_Wgenome0011P3_2013-05-18_079
A10_Wgenome0011P3_2013-05-18_080
A11_Wgenome0011P3_2013-05-18_095
A12_Wgenome0011P3_2013-05-18_096
same is the case with other series I,e.BCDEFGH
all I want to rename these file names in following format.
TaeCsp2AS_1635A01
TaeCsp2AS_1635A02
TaeCsp2AS_1635A03
TaeCsp2AS_1635A04
TaeCsp2AS_1635A05
TaeCsp2AS_1635A06
TaeCsp2AS_1635A07
TaeCsp2AS_1635A08
TaeCsp2AS_1635A09
TaeCsp2AS_1635A10
TaeCsp2AS_1635A11
TaeCsp2AS_1635A12


so please suggest me some command or script to do it........thanks in advance


Laurent_R
Veteran / Moderator

Jun 28, 2013, 3:49 AM

Post #2 of 3 (313 views)
Re: [meetvivek72] Renaming whole group of files with fix naming format [In reply to] Can't Post

Hi,

You don't give enough information on how the files other than A* should be renamed. Limiting the solution to the A* files, you could try this:

perl -e 'for (glob("A*")) {$nr=~ $1 if /.(\d\d)/; $new= "TaeCsp2AS_1635A$1"; rename $_, $new;}'

Make sure you test it thoroughly before you execute it. I have tested on only to files that were properly renamed, but a more detailed test is necessary if your data is important.

The syntax above is for Unix/Linux platforms, a couple of changes would be necessary on other OS.


BillKSmith
Veteran

Jun 28, 2013, 7:30 AM

Post #3 of 3 (301 views)
Re: [meetvivek72] Renaming whole group of files with fix naming format [In reply to] Can't Post

Laurent's suggestion is excellent if you only have to run this job a few times. If you have many directories, or if you will have to repeat it in the future, I recommend a more robust solution. (If for no other reason, a "one-liner" increases the risk due to typing errors.)

It may be overkill in your case, but a production version of this program should include a great deal of error detection and possibly recovery.

  • Verify that the directory exists.

  • Set the working directory to it

  • Verify that it contains all required files (and no others)

  • Create a temporary directory

  • Copy the required files to the .temp directory

  • Verify that the copy is correct

  • Rename the files in the temp directory

  • Verify that the rename succeeded.

  • Optionally delete the original files and copy the new ones back into the original directory.


  • Note that, at least on Unix, this is not exactly the same as renaming the files. This algorithm would lose all of the original file times
    Good Luck,
    Bill

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives