Το script-άκι μετονομάζει τα ελληνικά σε greeklish, μετατρέπει τα κεφαλαία σε μικρά και τα κενά σε underscores για όλα τα filenames, directory names ενός directory και μετά κάνει το ίδιο σε όλα τα directories ενός direcrory:

π.χ.
$./cleanfnamerec example_dir/
Filename example_dir has not been changed.
Filename example_dir/elliniko_filename_toy_dir has not been changed.
Filename example_dir/elliniko_filename_toy_dir/mpe has not been changed.
Filename example_dir/elliniko_filename_toy_dir/mpe/ελλάς has been changed to example_dir/elliniko_filename_toy_dir/mpe/ellas.

Το script υπάρχει εδώ: cleafnamrec script

και ο κώδικας είναι:

#!/usr/bin/perl

################################
# cleanfnamerec.pl
# author: Eleni Maria Stea
################################

use strict;
use Encode;
use File::Copy;
use File::Find::Rule;

my $dir;
my @files;
my $file;
my $initfile;
my $flag;

$dir=$ARGV[0];

if ( -d $dir ) {
    @files = File::Find::Rule->in($dir);
	foreach ( @files ) {
		next if ( $_ eq "." || $_ eq ".." );
		$file = $_;
		$initfile = $_;
		$flag = decode ("utf8",$_,Encode::FB_QUIET);
		if ( $flag ) {
			$file = encode("iso-8859-7",$flag);
		}
		$file =~ s/\s/_/g;
		$file =~ s/θ/th/g;
		$file =~ s/Θ/th/g;
		$file =~ s/ψ/ps/g;
		$file =~ s/Ψ/ps/g;
		$file =~ tr/ΑΒΓΔΕΖΗΙΚΛΜΝΞΟΠΡΣΤΥΦΧΩΆΈΉΊΎΌΏΪΫαβγδεζηικλμνξοπρστυφχωάέήίύόώϋϊΰΐς/abgdeziiklmnxoprstyfhoaeiiyooiyabgdeziiklmnxoprstyfhoaeiiyooyiyis/;
		$file =~s/_-_/-/g;
		$file = "\L$file";
		move("$initfile","$file") || die $!;
		if ( $initfile eq $file ) {
			print "Filename $initfile has not been changed.\n";
		}
		else {
			print "Filename $initfile has been changed to $file.\n";
		}
	}
}
else {
	print "Sorry dude. There is no such directory.\n";
}

3 σχόλια σε “Recursive Cleanup Filenames (perl script)”


  1. Cool script :)

    A slightly more functional approach, one that I often find cleaner to read after a few months have passed, is shown at:

    http://people.freebsd.org/~keramida/hikiko-cleanfname.perl.txt

    It is quite obvious from that variation of the same script that I prefer short, easy to understand and hack functions and a “LEGO style” of programming Perl, where functions are pluggable blocks of code that are (re-)attached in arbitrary ways to do amusing things.

    NOTE: There’s something wrong with my encodename() function but I find “perldoc Encode” too confusing to stay up at 01:16am and debug this. I’m sure you can fix that :-)

    • hikiko Λέει:

      LOL γιατί τα λες αγγλικά; :-) Το ricudi βανδαλίζει, το keramidi απαντάει μόνο αγγλικά… πλάκα έχετε :-) Πραγματικά ο κώδικάς σου έχει πολύ ωραία δομή, μάλλον από μόνη μου δε θα σκεφτόμουν να βάλω τόσα functions για μια αλλαγή στο encoding και μια αντικατάσταση στα γράμματα!! Thnx για το comment!!! Στο επόμενο script-άκι μου, μπορεί να δοκιμάσω κι εγώ να παίξω με τα LEGO!!!! :-D

  2. keramida Λέει:

    Από εκεί να καταλάβεις πόσο νύσταζα, που δεν είδα τι γλώσσα έγραφα :P


Υποβολή απάντησης