#!/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"); @thefiles=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 @thefiles=sort(@thefiles); #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(@thefiles) { unless ( ($f eq ".") || ($f eq "..") ) { $totalcount++; print " $totalcount. $f\n"; } } #Start the sort print "\n\n Begining sort of $totalcount files\n"; foreach $f(@thefiles) { 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";