LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2011, 08:51 PM   #1
pablgonz
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Rep: Reputation: Disabled
rename files in folders using perl script


I need to create a perl script for the following, I have an images folder, creates for other script, with the following files (creates whit script):
Code:
test-tmp-000001.ppm
test-tmp-000002.ppm
test-tmp-000003.ppm
test-tmp-000004.ppm
test-tmp-000005.ppm
and I want to stay in the following way
Quote:
test-tmp-1.ppm
test-tmp-2.ppm
test-tmp-3.ppm
test-tmp-4.ppm
test-tmp-5.ppm
but, using a perl script to open the directory and rename files only. ppm (in the directory there are more files with the same names and other extensions), dealing with the following code:
Code:
my $dir_renombra = "$tempDir/$imageDir";
my $fichero = '';
my $resultado = '';
my $var = 1;
if(opendir(DIR,$dir_renombra)){
  foreach (readdir DIR){
        $fichero = $_;
        if ($fichero =~ /($name-tmp-)\d+.ppm/)
        {
        next if $fichero=~/($name-tmp-$var)\.ppm/;
             my $nuevo_nombre = substr($fichero,0,-10) . "$var.ppm";
             $var = $var + 1;
             $resultado = rename("$dir_renombra/$fichero","$dir_renombra/$nuevo_nombre");
             print "Renaming $fichero to $nuevo_nombre: $resultado : $!\n";
        }
      }
    print "FIN\n";
   }
  else
   {
   print "dont open".$dir_renombra;
   }
closedir DIR;
but it throws the error and rename all the files. ppm to find.
If I puden assist with this, I would be very grateful or display an easier way to do it thanks.
Pablo.

Last edited by pablgonz; 11-24-2011 at 05:42 PM.
 
Old 11-23-2011, 10:15 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

You can use groups (parentheses) in regular expressions to extract matching substrings. See this link for example and play with the following code
Code:
my $dir_renombra = "$tempDir/$imageDir";
my $fichero = '';
my $resultado = '';
my $var = 1;
if(opendir(DIR,$dir_renombra)){
  foreach (readdir DIR){
        $fichero = $_;
        if ($fichero =~ /([^\d]+)(\d+).ppm/)
        {
	     my $var=int($2);
	     my $nuevo_nombre ="$1$var.ppm";
             print "Renaming $fichero to $nuevo_nombre\n";
        }
      }
    print "FIN\n";
   }
  else
   {
   print "dont open".$dir_renombra;
   }
closedir DIR;
You should add actual renaming to the code.

Hope this helps.

Last edited by firstfire; 11-23-2011 at 10:17 PM.
 
Old 11-24-2011, 03:20 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Hmm, the art of perl is to minimize typing:

Code:
foreach (<test-tmp-*.ppm>) {
    print "mv $_ ", s/0+// && $_, "\n";
}
 
Old 11-24-2011, 04:23 AM   #4
pablgonz
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by firstfire View Post
Hi.

You can use groups (parentheses) in regular expressions to extract matching substrings. See this link for example and play with the following code
....
You should add actual renaming to the code.
Hope this helps.
Thanks, I made ​​the changes you indicated, but, although the re display shows file names, but in reality (in the folder) not modify.
 
Old 11-24-2011, 04:37 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by pablgonz View Post
but in reality (in the folder) not modify.
That is why i wrote
Quote:
You should add actual renaming to the code.
This part (renaming) was implemented in your first program. Just copy it.
 
1 members found this post helpful.
Old 11-24-2011, 05:14 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
safest is to copy the output then look at it and run it.
that's why I use a "print" statement rather than do the
move, it's safer.

my_script.pl | tee ~/1

cat ~/1
[take a look at it]
Code:
mv test-tmp-000001.ppm test-tmp-1.ppm
mv test-tmp-000002.ppm test-tmp-2.ppm
mv test-tmp-000003.ppm test-tmp-3.ppm
mv test-tmp-000004.ppm test-tmp-4.ppm
mv test-tmp-000005.ppm test-tmp-5.ppm
It looks ok so run it as a shell script...

sh ~/1

Last edited by bigearsbilly; 11-24-2011 at 05:34 AM. Reason: explain
 
Old 11-24-2011, 07:45 AM   #7
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
zero-padded images filenames look better to me, if you want to do animations, diaporama, it will be easier to have them in correct sequence order
 
Old 11-24-2011, 05:40 PM   #8
pablgonz
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thanks, the rename fThank you very much, the script works perfectly, I had forgotten to print a line (I was a little sleepy)
 
Old 12-17-2011, 02:03 PM   #9
pablgonz
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
modification

I am making some modifications to the script (adding a few features) and I encountered the following problem, in some cases files are created as follows
Code:
test1-tmp-000001.ppm
test1-tmp-000002.ppm
test1-tmp-000003.ppm
or
Code:
test1-tmp-1-000001.ppm 
test1-tmp-2-000002.ppm 
test1-tmp-3-000003.ppm
or
Code:
test1-tmp-1-1.ppm 
test1-tmp-2-1.ppm 
test1-tmp-3-1.ppm
my question is that I should change in line
Code:
if ($fichero =~ /([^\d]+)(\d+).ppm/)
to re name in either case files to
Code:
test1-tmp-1.ppm 
test1-tmp-2.ppm 
test1-tmp-3.ppm
In simpler terms i want to save at test1-tmp- in $1 and the rest in $2
thanks for your help

Last edited by pablgonz; 12-17-2011 at 10:47 PM. Reason: comments
 
Old 12-17-2011, 10:20 PM   #10
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

I see the problem.. Try the following regular expression
Code:
if ($fichero =~ /(.+-)(\d+).ppm/)
First group matches (almost) any string ending with the dash "-". Previously the first group matched only a string of non-digits.

If you want to use filenames like "test-tmp_000001.ppm" then change regexp as follows
Code:
if ($fichero =~ /(.+[^\d])(\d+).ppm/)

Last edited by firstfire; 12-17-2011 at 10:25 PM.
 
Old 12-17-2011, 10:51 PM   #11
pablgonz
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
thanks for the answer, change the line to
Code:
if ($fichero =~ /(.+[^\d])(\d+).ppm/)
but fails if the files are of the form
Code:
test1-tmp-1-000001.ppm 
test1-tmp-2-000002.ppm 
test1-tmp-3-000003.ppm

Last edited by pablgonz; 12-17-2011 at 10:53 PM.
 
Old 12-17-2011, 11:23 PM   #12
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Code:
$ perl rename.pl 
Renaming test-tmp-000005.ppm to test-tmp-5.ppm
Renaming test-tmp-000003.ppm to test-tmp-3.ppm
Renaming test1-tmp-000001.ppm to test1-tmp-1.ppm
Renaming test-tmp-000001.ppm to test-tmp-1.ppm
Renaming test-tmp-000002.ppm to test-tmp-2.ppm
Renaming test-tmp-000004.ppm to test-tmp-4.ppm
Renaming test1-tmp-2-000001.ppm to test1-tmp-2-1.ppm
FIN
What is the desired output? How the program is supposed to decide which number to keep?

Last edited by firstfire; 12-17-2011 at 11:28 PM.
 
Old 12-18-2011, 12:00 AM   #13
pablgonz
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by firstfire View Post
Hi.
What is the desired output? How the program is supposed to decide which number to keep?
Thanks for the reply, I was trying something like this
Code:
if ( $fichero =~ /(save in $1)(\d+|\d+[-]\d+).ppm/)
In theory it should save test-tmp- in $1 and the rest (all case) in $2

Last edited by pablgonz; 12-18-2011 at 12:00 AM. Reason: corrections
 
Old 12-18-2011, 01:21 AM   #14
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Code:
if ($fichero =~ /(.+?-)((\d+-?)+).ppm/)
'.+?' is a non-greedy regular expression.
Example:
Code:
$ echo test1-tmp-2-000001.ppm | perl -ne '/(.+?-)((\d+.+-?)+).ppm/;print "[$1][$2]\n"'
[test1-tmp-][2-000001]
Note that in this case $2 may be something like '2-000001' and int(2-000001) -> 2. This may be a problem, because int(2-000005) -> 2 as well.

Last edited by firstfire; 12-18-2011 at 01:23 AM.
 
Old 12-18-2011, 03:01 AM   #15
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Are you aware that there's already a perfectly good perl renaming script available? It comes bundled with the perl package in debian-based systems (at least), or you can get it here:

https://gist.github.com/995151

With it, renaming the files listed above would be as simple as:

Code:
prename 's/-0{5}/-/' *.ppm
Modify the expression as necessary, of course. Ensuring that you're renaming the correct files is just a matter of making sure you use regex and globbing expressions that accurately target only the pattens you want.

Even if prename isn't exactly what you're looking for, you may be able to look at its code for more tips.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for help with Bash script to search folders, rename, and output to text file ExitRitual Programming 8 01-05-2011 07:31 AM
Need to rename files and folders with invalid characters in the names laureynsr Linux - Newbie 5 04-01-2009 03:13 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
Rename multiple files in folders??? adds2one Linux - Software 19 10-05-2006 12:22 AM
need a perl script to remove folders cccc Programming 5 03-12-2004 02:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration