|
|
Question : randomize filenames
|
|
I need a script to read all the image filenames of a directory into an array (jpg files ONLY) and then:
randomize the name of each file. Requirements:
each file should start off with the same common prefix such as 'mo'. Following 'mo' should be another 8 random characters (numbers or letters, capital or lower case). Just in case the same random set of characters (however unlikely it is) is used for more than 1 file, i need to do a check to prevent the same random set of characters is used before changing the filename. Each file should have the jpg extension.
example:
file 1) mo5yM9QauM.jpg file 2) moj0NaebV7.jpg file 3) mohV9kL15c.jpg ...etc.
Thanks!
|
Answer : randomize filenames
|
|
#/usr/bin/perl use MD5; foreach $file (<*.jpg>) {
for(;;) { $newname = "mo".(uc substr MD5->hexhash($file.rand()),0,8)."jpg" ; last unless (-e $newname); } rename $file ,$newname; }
implementation of above algo... hope it was timely.
|
|
|
|
|