[SOLVED] rename files in folders using perl script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
$ 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?
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.