This perl program renumbers files by their creation date. Possible arguments are file-extension and basename.

Examples:

  • daterename -e jpg -n "pic_"
  • daterename -e jpg -n ""
  1. #!/usr/bin/perl -w
  2. use Cwd;
  3. use strict;
  4. use warnings;
  5.  
  6. sub daterename($$)
  7. {
  8. my $type = shift;
  9. my $name = shift;
  10. my $path = getcwd();
  11. my %fhash;
  12. my $muster = $path."/*.".$type;
  13. my @globbing = glob($muster);
  14. my $glength = @globbing;
  15.  
  16. unless ($glength == 0)
  17. {
  18. foreach my $filename (@globbing)
  19. {
  20. my @a = stat($filename);
  21. $fhash{"$filename"} = $a[9];
  22. }
  23. my $index = 1;
  24. foreach my $file
  25. (
  26. sort{$fhash{$a} cmp $fhash{$b}}
  27. keys %fhash
  28. )
  29. {
  30. print "rename $file\t to $name$index\n";
  31. rename($file, $name.$index.".".$type);
  32. $index++;
  33. }
  34. }
  35. else
  36. {
  37. print "Nothing to do\n";
  38. }
  39. }
  40.  
  41. sub help()
  42. {
  43. print "usage of daterename:\n";
  44. print " required parameters:\n";
  45. print " -n basename of the files\n";
  46. print " -e file-extension (like jpg...)\n";
  47. print " optional parameters:\n";
  48. print " none at the moment\n";
  49. }
  50.  
  51. my $type = "null";
  52. my $name = "null";
  53. my $l = @ARGV;
  54.  
  55. for(my $i = 0; $i < $l ; $i++)
  56. {
  57. if($ARGV[$i] =~ /^-n$/)
  58. {
  59. if(defined $ARGV[$i+1])
  60. {
  61. $name = $ARGV[$i+1];
  62. $i++;
  63. }
  64. else
  65. {
  66. help();
  67. last;
  68. }
  69. }
  70. elsif($ARGV[$i] =~ /^-e$/)
  71. {
  72. if(defined $ARGV[$i+1])
  73. {
  74. $type = $ARGV[$i+1];
  75. $i++;
  76. }
  77. else
  78. {
  79. help();
  80. last;
  81. }
  82. }
  83. }
  84.  
  85. if($type ne "null" and $name ne "null")
  86. {
  87. daterename($type,$name);
  88. }
  89. else
  90. {
  91. help();
  92. }