Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.85 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Find;
  5.  
  6. my @file_list;
  7. my @path_list;
  8.  
  9. finddepth (\&wanted, ".");
  10.  
  11. #removes dot element indicating current dir
  12. @file_list = File::Spec->no_upwards( @file_list );
  13. my @name_list;
  14. foreach my $file (@file_list)
  15. {
  16.     my @tmp_name;
  17.     my @name;
  18.     my @parts;
  19.     my $tmp_file;
  20.     my $dir_chk = 1;
  21.     ($tmp_file = $file) =~ tr/ _[]()/../d;
  22.     #split name into pieces using dot as deliminator
  23.     push @tmp_name, split(/\./,$tmp_file);
  24.     unless ( (-d $file || @tmp_name == 1) )
  25.     {
  26.         #skip last element contqining filetype e.g. ("txt")
  27.         @name = @tmp_name[0..$#tmp_name-1];
  28.         $dir_chk = 0;
  29.     }
  30.     else
  31.     {
  32.         @name = @tmp_name;
  33.     }
  34.     foreach my $piece (@name)
  35.     {
  36.         #unless contains numbers "and" letters shorten to 1 char
  37.         unless ( ($piece =~ /[[:alpha:]]/ && $piece =~ /[[:digit:]]/) )
  38.         {
  39.             $piece = substr $piece, 0, 1;
  40.         }
  41.         push @parts, $piece;
  42.     }
  43.     unless($dir_chk =~ 1)
  44.     {
  45.         #add last element containing filetype back on
  46.         push @parts, $tmp_name[-1];
  47.     }
  48.     #add the name to new array as 1 element
  49.     push @name_list, join('.',@parts);
  50. }
  51.  
  52. #if its been seen before append 1
  53. #if its seen 2nd time get number from end add 1 and append it
  54. my %h;
  55. foreach (@name_list)
  56. {
  57.     s/(\d*)$/($1||0) + 1/e while $h{$_}; $h{$_}++;
  58. }
  59.  
  60. #becuase last element is dot
  61. pop @path_list;
  62.  
  63. my @file_name = @path_list;
  64. my @new_name = @path_list;
  65. #append element 0 to element 0 etc. using path to keep order
  66. for my $i (0..$#path_list)
  67. {
  68.     $file_name[$i] .= $file_list[$i];
  69.     $new_name[$i] .= $name_list[$i];
  70. }
  71.  
  72. while ( @file_name and @new_name )
  73. {
  74.     rename shift @file_name, shift @new_name;
  75. }
  76.  
  77. sub wanted
  78. {
  79.     push @file_list, $_;
  80.     push @path_list, "$File::Find::dir/";
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement