Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use File::Find;
- my @file_list;
- my @path_list;
- finddepth (\&wanted, ".");
- #removes dot element indicating current dir
- @file_list = File::Spec->no_upwards( @file_list );
- my @name_list;
- foreach my $file (@file_list)
- {
- my @tmp_name;
- my @name;
- my @parts;
- my $tmp_file;
- my $dir_chk = 1;
- ($tmp_file = $file) =~ tr/ _[]()/../d;
- #split name into pieces using dot as deliminator
- push @tmp_name, split(/\./,$tmp_file);
- unless ( (-d $file || @tmp_name == 1) )
- {
- #skip last element containing filetype e.g. ("txt")
- @name = @tmp_name[0..$#tmp_name-1];
- $dir_chk = 0;
- }
- else
- {
- @name = @tmp_name;
- }
- foreach my $piece (@name)
- {
- #unless contains numbers "and" letters shorten to 1 char
- unless ( ($piece =~ /[[:alpha:]]/ && $piece =~ /[[:digit:]]/) )
- {
- $piece = substr $piece, 0, 1;
- }
- push @parts, $piece;
- }
- unless($dir_chk =~ 1)
- {
- #add last element containing filetype back on
- push @parts, $tmp_name[-1];
- }
- #add the name to new array as 1 element
- push @name_list, join('.',@parts);
- }
- #if its been seen before append 1
- #if its seen 2nd time get number from end add 1 and append it
- my %h;
- foreach (@name_list)
- {
- s/(\d*)$/($1||0) + 1/e while $h{$_}; $h{$_}++;
- }
- #becuase last element is dot
- pop @path_list;
- my @file_name = @path_list;
- my @new_name = @path_list;
- #append element 0 to element 0 etc. using path to keep order
- for my $i (0..$#path_list)
- {
- $file_name[$i] .= $file_list[$i];
- $new_name[$i] .= $name_list[$i];
- }
- while ( @file_name and @new_name )
- {
- rename shift @file_name, shift @new_name;
- }
- sub wanted
- {
- push @file_list, $_;
- push @path_list, "$File::Find::dir/";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement