#! /usr/bin/perl
use strict;
#####use CGI ':standard'; (ON A WEB BASED SERVER, MUST DECLARE YOU ARE USING CGI STANDARD)
#####print "Content-type: text/html\n\n"; (NO NEED TO DECLARE CONTENT TYPE)
my ($time_date, $forward_slash, $duplicate_file_indicator, $fullstop, $path, $no_mime_folder, @directory_contents, @mime_type_split, $mime_type_split_scaler, @mime_type, $number_of_files, $counter, $move_to_folder, $path_2, $file_name, @file_exist_split, $file_exist_shift, $file_exist_mime);
my ($sec, $min, $hour, $mday, $mon, $year)=gmtime;
$time_date = sprintf ('%02d%02d%02d%02d%02d%02d', $hour, $min, $sec, $mday, $mon, $year);
$forward_slash = "/";
$duplicate_file_indicator = "_$time_date";
$fullstop = ".";
$path = "/ChrisMassey.co.uk/Perl/Scripts/CleanDirectory/Files/";
$no_mime_folder = "NoMimeDump";
opendir (LOGDIR, "$path") || die "Cannot Open: $path";
@directory_contents = readdir (LOGDIR);
closedir (LOGDIR);
#####shift (@directory_contents); (ON A WEB BASED SERVER, SOMETIMES THE FIRST VALUES ARE A . AND A ..) (REMOVES THE .)
#####shift (@directory_contents); (REMOVES THE ..)
$number_of_files = @directory_contents;
if ($number_of_files != 0) {
foreach (@directory_contents) {
if ($_, /\.$/) {
unless (-e "$path$no_mime_folder") {
mkdir ("$path$no_mime_folder", 0777) || die "Cannot Open: $path$no_mime_folder";
}
rename ("$path$_", "$path$no_mime_folder$forward_slash$_") || die "Cannot Open: $path$_ - $path$no_mime_folder$_";
print "<p>$_ has been moved to $path$no_mime_folder";
}
@mime_type_split = split (/\./, $_);
shift (@mime_type_split);
$mime_type_split_scaler = "@mime_type_split";
push @mime_type, $mime_type_split_scaler;
}
foreach (@mime_type) {
unless (-e "$path$_") {
mkdir ("$path$_", 0777) || die "Cannot Open: $path$_";
}
}
$counter = 0;
while ($counter < $number_of_files) {
foreach (@directory_contents) {
$move_to_folder = @mime_type[$counter];
unless (-d "$path$move_to_folder$forward_slash$_") {
$path_2 = $path . $_;
$file_name = $_;
if (-e "$path$move_to_folder$forward_slash$_") {
@file_exist_split = split (/\./, $_);
$file_exist_shift = shift (@file_exist_split);
$file_exist_mime = "@file_exist_split";
shift (@file_exist_split);
$_ = $file_exist_shift . $duplicate_file_indicator . $fullstop . $file_exist_mime;
}
rename ("$path_2", "$path$move_to_folder$forward_slash$_") || die "Cannot Open: $path$_ - $path$move_to_folder";
print "<p>$file_name has been moved to $path$move_to_folder";
$counter++
}
}
}
print "<p>Directory $path Was Empty...";
}
if ($number_of_files == 0) {
#####print "Content-type: text/html\n\n"; (NO NEED TO DECLARE CONTENT TYPE)
print "<p>Process Complete...";
}