# Last update: 2005/12/30 # # Automatic namer for only-number-jpg-files and string-prefixed-numered jpg-files # # (p) Jerry Nagasaki # # # Renames only-number- or string-prefixed-jpg-files (e. g. 01.jpg, or pp01.jpg, or ...) # to files with 5 random leading characters # # INPUT: File names like 01.jpg, 02.jpg, 03.jpg, ... or: [ABC]01.jpg, ..., whereby # [ABC] is argument of script call # OUTPUT: Renamed file names to ABCD01.jpg, ABCD02.jpg, ABCD03.jpg, ... whereby # the first 5 characters are randomly generated per file list and a sub # set of [0-9][A-Z][a-z] # # Note: File names smaller than 10 w/o leading zero will also be renamed to # ABCD0n.jpg, n = {0, 1, 2, ... , 9} # ^ # # get argument for starting characters, if existing $prefix = $ARGV[0] || ""; # maximum file numbers to search $MAXFILES = 99; # file extension for renaming $FILE_EXT = ".jpg"; # definition to set length to "zero" $code =""; # get randomized values and continue until 5 ASCII characters got while (length($code) < 5) { $rand = chr(int(rand(1) * 256)); # add only 0-9, A-Z, or a-z to string if ($rand =~ /[0-9A-Za-z]/) { $code .= $rand; } } # rename files with "'just-a-number'.jpg" to "'5-randomized-characters-plus-number'.jpg" for ($i = 0; $i < $MAXFILES; $i++) { $filename1 = $i.$FILE_EXT; # prepare for checking for no leading "0" -> "n" $filename2 = "0".$i.$FILE_EXT; # prepare for checking for leading "0" -> "0n" $filename3 = "00".$i.$FILE_EXT; # prepare for checking for leading "00" -> "00n" # check for "n" or "0n" ... if ((-e $prefix.$filename1) && ($i < 10)) { $newname = "$code"."0"."$filename1"; # add a "0", if "n.jpg" print "Renaming $prefix$filename1 to $newname\n"; rename($prefix.$filename1, $newname); } elsif ((-e $prefix.$filename2) && ($i < 10)) { $newname = "$code$filename2"; print "Renaming $prefix$filename2 to $newname\n"; # don't add a "0", if "0n.jpg" rename($prefix.$filename2, $newname); } elsif ((-e $prefix.$filename2) && ($i >= 10)) { $newname = "$code$filename2"; print "Renaming $prefix$filename2 to $newname\n"; # add a "0", if "0n.jpg", n >= 10 rename($prefix.$filename2, $newname); } elsif ((-e $prefix.$filename3) && ($i < 10)) { $newname = "$code$filename3"; print "Renaming $prefix$filename3 to $newname\n"; # don't add a "0", if "0n.jpg" rename($prefix.$filename3, $newname); } elsif (-e $prefix.$filename1) { $newname = "$code$filename1"; print "Renaming $prefix$filename1 to $newname\n"; rename($prefix.$filename1, $newname); } }