Sort Perl Script
Click here for a text file of this script, description below
I wrote this script to sort a huge collection (2500+) of photos that were sent to me as a *.zip file which didn't have any directory structure information.
The images were survey shots which were named by numbered site and then described, so one of the shots for site ABC which was of a tree would be named ABC_photo-of-tree.jpg. I wanted each set of photos sorted into folders by site name, so I wrote this script to read in the file name, create a folder based on whatever came before the underscore (in the above example the folder would be ABC) and then move the photos into it. It took a few minutes to write, and the whole set of photos were then sorted within seconds.
The script can of course be re-written to use any delimiter.
Sort Script
At the top of the page can be found a link to a text file of this script.
#!/usr/bin/perl use File::Copy; #This script was written by Edwin Hopper - http://www.shappyhopper.co.uk #This script looks for a delimiter in a file name and sorts the files accordingly #In this case it's looking for an underscore and will move the file to a folder named for whatever text came before the underscore #So if the file was called ABC_123.doc it will be moved to a folder called ABC #First we tell the user what we are doing print "\n\n#############################################################################"; print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!Starting Sort!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n"; print " This may take some time depending on the size\n"; print " and number of files."; # Then We Open the local directory, read all the file names and populate an array # $DirToGet="."; opendir(IMD, $DirToGet) || die("Cannot Open Directory"); =readdir(IMD); closedir(IMD); #Sort array alphabetically #THIS IS VERY IMPORTANT #IF MODIFYING THIS SCRIPT REMEMBER THAT WITHOUT THE SORT FUNCTION FOLDER NAMES WILL BE OVERWRITTEN =sort(); #Count how many files there are print "\n\n\n Counting files in directory."; #List the contents of the Directory print "\n This script has found the following files in the target directory:\n"; $totalcount=0; foreach $f() { unless ( ($f eq ".") || ($f eq "..") ) { $totalcount++; print " $totalcount. $f\n"; } } #Start the sort print "\n\n Begining sort of $totalcount files\n"; foreach $f() { unless ( ($f eq ".") || ($f eq "..") ) { $counteach++; #This section works out the file names print " Moving $f (file $counteach of $totalcount)\n"; ###############In this section we use the split method to get file names based around delimiter. Change the delimiter to suit your files ($foldername, $restoffilename)=split(/_/,$f); ################################################################################################################################ #Here we work out the old and new file names $oldlocation="./$f"; $newlocation="./$foldername/$f"; #We make the new directory mkdir $foldername; #Here we move the file move($oldlocation, $newlocation); } } print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!Sort complete!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"; print "#############################################################################\n\n";