#!/usr/bin/perl -i_bak
use
warnings;use
strict; use
File::stat;use
File::Basename;use
Net::FTP;use
POSIX;use
File::Copy "cp";
#use Win32::CopyFile;
# Hash of arrays
while
(1) {
my $subdirs; my $totalfiles = 0; my $sumsizefiles; my $dname = "C:\\Folder\\"; my $localroot = "C:\\Folder.Root"; opendir(INPUT, $dname) or die "Can't open directory $dname: $!"; my $file; my @fields;
# Set up Console for Windows or UNIX OS
my ($screen_clear, $file_remove, $file_copy, $file_move); my $OSNAME = $^O; if ( $OSNAME =~ /MSWin32/) {
# Set up Windows variables $
screen_clear = "cls"; $file_remove = "del"; $file_copy = "copy"; $file_move = "move"; }
else { # Set up UNIX variables $
screen_clear = "clear"; $file_remove = "rm"; $file_copy = "cp"; $file_move = "rm"; }
system("$screen_clear"); my %content= (); my (@str_DOW, @str_Month, @int_Day, @str_Time, @int_Year) = (); my (@TotalArray, @FilesArray, @SizeArray) = (); # Overwrite dirfiles.txt by creating a new empty file
# Setup OUTPUT to append hash data to the new file in the hash loop local *OUTPUT; open(OUTPUT, ">$localroot\\dirfiles.txt") || die "Can't open output file: $!"; open(OUTPUT, "+>>$localroot\\dirfiles.txt") || die "Can't open output file: $!"; while ( $file = readdir(INPUT) )
{
my $pathtofiles = "$dname\\$file";
$subdirs++ and next if -d $pathtofiles; my $sb = stat($pathtofiles); $sumsizefiles += $sb->size; $totalfiles++;
# Split the time string and create array to sort by date
my @time = scalar localtime $sb->mtime; (
@str_DOW, @str_Month, @int_Day, @str_Time, @int_Year) = split /:\s*/, @time, 5; $TotalArray[$totalfiles] = $totalfiles; # Construct an hash of each line created by the loop
%
content = (Key => $TotalArray[$totalfiles], Time => $str_Time[$totalfiles], DOW => $str_DOW[$totalfiles], Month => $str_Month[$totalfiles], Day => $int_Day[$totalfiles], Year => $int_Year[$totalfiles], File => $FilesArray[$totalfiles], Size => $SizeArray[$totalfiles],); my @slice = (); @slice = @content{"Key","Time","DOW", "Month","Day","Year","File","Size"}; my $newline = 0;
# Simultaneously append (build) the list by printing OUTPUT to file
if ($newline % 8 == 0) {
# print OUTPUT "$content{Key}:\t$content{Time}\t$content{Day}\t$content{Month}\t$content{Year}\t$content{DOW}\t$content{File}\t$content{Size}\n";
print OUTPUT @slice; $newline = 0; }
else { print OUTPUT "\n"; $newline++;
}
# Make a copy of dirfiles.txt in same directory location syscopy
("$localroot\\dirfiles.txt", "$localroot\\dirfilessorted.txt");
# Open second read from new dirfiles.txt to sort and save to in the
# same directory location local *INPUT; open(INPUT, ">$localroot\\dirfilessorted.txt") || die "Can't open output file: $!"; open(INPUT, "+>>$localroot\\dirfilessorted.txt") || die "Can't open output file: $!"; # Sort the hash by number of members and name
foreach $totalfiles (sort { $a cmp $b } keys %content) { print OUTPUT "$content{Key}:\t$content{Time}\t$content{Day}\t$content{Month}\t$content{Year}\t$content{DOW}\t$content{File}\t$content{Size}\n"; } # Print unsorted directory content, line by line to STDIO printf "%s %8.0f %s \n", $file, $sb->size, scalar localtime $sb->mtime; }
closedir(INPUT);# Print the total files and sum of file sizes printf "\n%4d File(s) %d KB\n", $totalfiles, $sumsizefiles/1024;print "Not including $subdirs Sub Directories\n";close(OUTPUT) || die "Can't close my file: $!";sleep 10;
}