This is the old SliTaz forum - Please use the main forum.slitaz.org

[Perl] renup.pl : renaming and removing for better sorting
  • babaorumbabaorum March 2010
    Hi again, here is another renaming script that is much more oriented on purpose to sort files by name, in same directory, when they are scattered in several num-base named subdirectories.
    It is useful to view a large amount of images from comics/manga scans, as an example, or family photo shots (or not so homely ones... 0_õ ), when they are already num-base named but grouped by small series in many subdirectories (which is highly uncomfortable to browse w/ simple image viewers like GPicView).
    The goal is to begin w/ such a directory structure:
    directory
    + subdirectory01
    - file01.png
    - file02.png
    + subdirectory02
    - file01.png
    - file02.png
    - file03.png
    - file04.png

    and transform it in such a directory structure:
    directory
    - [user-prefix]01_file01.png
    - [user-prefix]01_file02.png
    - [user-prefix]02_file01.png
    - [user-prefix]02_file02.png
    - [user-prefix]02_file03.png
    - [user-prefix]02_file04.png

    The main action is divided is several subroutines:
    1) browse each subdirectory and parse the numerical part of their basename
    2) rename each file in each subdirectory by adding an overall prefix made from "[user-specified part][numerical part of subdir basename]_" (w/o " " of course)
    3) move all files one level up in root working directory and, if subdirectory empty, delete it.

    It is hardly based on another Perl script that I posted on this board, rename.pl.
    It uses the same -t option to simulate the main action in testmode rather than really doing possibly harmful things like renaming, moving and directory-deleting... ^^

    The user-prefix is either configured in script source (by assigning a litteral value to $prefix scalar) or asked before main process in script action.
    You can easily tweak the fixed regexp internally used w/ rename.pl in script source too.

    Please make sure to paste the code in a renup.pl file given executable user-permission (chmod +x renup.pl) and placed in one of the executable paths specified by shell environment $PATH variable.

    Here is the code:
    #!/usr/bin/perl -w
    chomp (my $scriptname = `basename "$0"`);
    chomp (my $workdir = `pwd`);
    my $help = <<HELP;
    Usage : $scriptname -h
    $scriptname [-t] [work-directory]
    HELP
    my $testmode = 0;
    my $prefix = "";
    #check dependencies
    die "Execution error: rename.pl needed but not found\n" if (`which rename.pl` eq '');
    #check parameters
    die $help if ($ARGV[0] eq '-h' or $ARGV[0] eq '--help');
    foreach my $a (@ARGV)
    {
    if ($a eq '-t') { $testmode = 1; }
    else
    {
    $a = $workdir.'/'.$a if ($a !~ /^\//);
    if(-d $a and -r $a) { $workdir = $a; }
    else { die "Syntax error: $a is not a valid directory\n$help"; }
    }
    }
    #asks a pattern for rename.pl if unknown
    if ($prefix eq '')
    {
    print "*** CAUTION: no prefix specified in substitution pattern for rename.pl\n";
    print "*** Please fill in a prefix once for the whole process\n";
    print "*** (press ENTER key to validate) : ";
    chomp ($prefix = <STDIN>);
    }
    #look for subdirectories in $workdir
    print "Scanning subdirectories in $workdir...\n";
    chdir $workdir;
    foreach $f (<*>)
    {
    my $path = $workdir.'/'.$f;
    if (-d $path)
    {
    chdir $path;
    my @all = <*>;
    my @regularfiles = grep { -f } @all;
    printf "\n> %s : %d files\n", $f, scalar @regularfiles;
    next if (scalar @regularfiles == 0); #skip if empty
    #parse numeric part of $path to use in rename.pl's pattern
    $path =~ /(\d+)/;
    my $num = $1;
    system 'rename.pl '.($testmode == 1? '-t ':'').'"s/^/'.$prefix.$num.'_/" *'; #call rename.pl
    #update files data (changed because of renaming)
    @all = <*>;
    @regularfiles = grep { -f } @all;
    map { rename $_, $workdir.'/'.$_ } @regularfiles unless ($testmode); #move files one level up
    #check for subdirectory inside
    if (scalar (grep { -d } @all) > 0) { warn "Caution: subdirectory found in $f => not deleted\n"; next; }
    chdir $workdir;
    unless ($testmode) { rmdir $path or die "$!\n"; }
    }
    }


    Short usage help is reached with renup.pl -h.

    I've written this script for my own usage and gets it out of the box as it is. I hope though it will be useful to some of you.

    => CAUTION <=
    I highly recommand that you firstly make large backup of your directory and parent directories before getting used to run this script.
    This script is written to potentially do very harmful actions like moving files in other directories and delete directories.
    Always specify a complete working directory path as a parameter before starting implicitely using current directory that script is called from.


    --Babaorum

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Apply for Membership

SliTaz Social