Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort a list of filenames by date modified...

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have the code below. I am trying to make it sort by the date modified. I am by no means a perl programmer so I basically have been banging my head against the wall for a month trying to get this to work. HELP!!!!!

Donnie

#!/usr/bin/perl
#
#
#########################
# This program will assemble 'on the fly' a web page full of images
# from a directory in your archive on your server. It is customizeable in that you
# use a pre-made HTML template for the output screen. You can make the template
#using all the colors and design of the rest of your site, making it fit right in.
#
# To install:
# 1. Edit the variables below.
# 2. Make a template web page. Use <--TITLE--> and <--IMAGES--> where you want
# the title and image to be inserted. It's OK to use a WYSIWYG editor. This script will
# weed out the < code.
# 3. Upload everything to it's place:
# The script: To your CGI BIN. Chmod 755
# The Template: To $TEMPLATE's path
# 4. Call the script with your browser: # 5. Note: if you do not have a template file, you will get an error.
########################################################
#
# NEW FEATURE:
# You can run this program stand alone, which will read the designated directory,
# Or you can run it to read different directories of images with a command line.
# # Where /image_dir/ is the dir you wish to display in relation to your public_html root.
# You must use the forward slashes around your image dir, or it will not work.
#
#########################################################
# E D I T V A R I A B L E S
#########################################################

$URL_ROOT = &quot;# The HTTP root of your site.

$PAGE_TITLE = &quot;<font face='Arial' size='4'><b>The Treasure Spot</b></font><font face='Arial' size='2'><br>Click thumbnail to see bigger picture, Click BACK to return to this page&quot;;
# The title of the results page. Set to &quot;&quot; for none.

$TEMPLATE= &quot;/usr/home/spot/public_html/new/ai/aitemplate.html&quot;;
# the complete UNIX path and filename of your template

# Thumbnail size: both width and height of 0=do not thumbnail (real mode)
$width =&quot;240&quot;;
$height = &quot;240&quot;;
#########################################################
#
# Edit These two if you wish to process one and only one dir.
#

$UNIX_PATH = &quot;&quot;;
# The unix path to the dir you wish to process.

$URL_PATH = &quot;&quot;;
# The HTTP equivelent of $UNIX_PATH- but with no trailing slash.

##########################################################
#
# Enter how many images to show per page.
#
$max_images = 8;


# E N D E D I T V A R I A B L E S
#########################################################

#################
# Add and calculate the links for more and less pages
#################

print &quot;Content-type: text/html\n\n&quot;;

#########################################
# Read and store the images in the dir
#########################################

$HTML_ROOT = &quot;$ENV{'DOCUMENT_ROOT'}&quot;;

#
# split up path to get information
@split_up = split (/&/, $ENV{'QUERY_STRING'});
$remote_path= $split_up[0]; # get path for images to be listed

if ($remote_path ne &quot;&quot;) {$UNIX_PATH = &quot;$HTML_ROOT&quot;.&quot;$remote_path&quot;;}

opendir(DIR,&quot;$UNIX_PATH&quot;) || &error (&quot;Cannot open $UNIX_PATH&quot;);


#####
# Sort
#####
my @unsorted = map { [ $_, (stat &quot;$logdir/$_&quot;)[7,9] ] } readdir DIR;
my @sorted = sort { -C $b->[2] <=> -C $a->[2] } @unsorted;

foreach (@sorted)
{
$file = $_->[0];
$lcfile = lc($file);
if (($lcfile =~ /.gif/) || ($lcfile =~ /.tif/) || ($lcfile =~ /.jpg/) || ($lcfile =~ /.jpeg/))
{
push (@dir_list, &quot;$file&quot;);
}
}
$count=@dir_list;
closedir(DIR);

# calculate image to start with here
if ($ENV{'QUERY_STRING'} =~ /&/) { $start_image = $split_up[1]; }
else { $start_image = 0; }

$total_images = $count;
$cgi_path = &quot;ai.cgi?$remote_path&quot;;

# add to the page title.
$PAGE_TITLE .= &quot;<br>&quot; . &CalculatePrevNextList ($start_image,$max_images,$total_images,$cgi_path) . &quot;<br>&quot; . &Counts($start_image,$max_images,$total_images);


##############################
# Read and store the template:
##############################

open(TEMPLATE_file,&quot;$TEMPLATE&quot;) || &error (&quot;Can't open $TEMPLATE\n&quot;);
@html=<TEMPLATE_file>;
close(TEMPLATE_file);

#############################
# Start html insert routine
#############################

if ($remote_path ne &quot;&quot;) {$URL_PATH = &quot;$URL_ROOT&quot;.&quot;$remote_path&quot;;}
$output = &quot;\n&quot;;
$image_count = 1; # only used for collecting correct images to show
$max_image_count = $start_image + $max_images; # only used for collect correct images to show
foreach $file(@dir_list)
{
if ($image_count > $start_image and $image_count <= $max_image_count)
{
if (($width eq &quot;0&quot;) && ($height eq &quot;0&quot;))
{
# got rid of url path from href
$output .= &quot;<a href=\&quot;$URL_PATH/$file\&quot;>\n<img src=\&quot;$URL_PATH/$file\&quot; ALT=\&quot;Filename: $file\&quot; border=0></a>\n&quot;;
}
else
{
$output .= &quot;<a href=\&quot;$URL_PATH/$file\&quot;>\n<img src=\&quot;$URL_PATH/$file\&quot; ALT=\&quot;Filename: $file\&quot; border=0 width=$width height=$height></a>\n&quot;;
}
}
$image_count ++;
}

#########################
# Now search and replace
#########################
foreach $line(@html)
{
$REP_TEXT=&quot;&quot;;
$REP_TEXT = &quot;<--TITLE-->&quot;;
$line =~ s/$REP_TEXT/$PAGE_TITLE/gi;

$REP_TEXT=&quot;<--TITLE-->&quot;;
$line =~ s/$REP_TEXT/$PAGE_TITLE/gi;

$REP_TEXT = &quot;<--IMAGES-->&quot;;
$line=~ s/$REP_TEXT/$output/gi;

$REP_TEXT=&quot;<--IMAGES-->&quot;;
$line =~ s/$REP_TEXT/$output/gi;

if (($line =~/<\/body>/) || ($line =~/<\/html>/)) {$line=&quot;&quot;;}

print &quot;$line&quot;;
}

print <<__HTML;

</body>
</html>
__HTML
exit;



######################
# ERROR HANDLING
######################
sub error
{
print &quot;<HTML><BODY>&quot;;
print &quot;<BOLD><p><FONT size=+1>Auto Image Error:<FONT COLOR=\&quot;#FF0000\&quot;> <B>$_[0]</B></FONT></FONT><BR>&quot;;
exit;
}

#
# Shows a summary of how many images in list
#
sub Counts {
my $StartWith = shift;
my $PerPage = shift;
my $TotalRecords = shift;

my $StartRecord = $StartWith + 1;
my $EndRecord = $StartWith + $PerPage;
if ($EndRecord > $TotalRecords) { $EndRecord = $TotalRecords; }
return &quot;$StartRecord-$EndRecord out of $TotalRecords&quot;;
}


#
# Calculate next and previous links
#
sub CalculatePrevNextList {
my $LimitNow = shift; ### Where the search is now.
my $PerPage = shift; ### Amount to show per page.
my $MaxRecords = shift; ### MaxRecords.
my $Link = shift; ### Link to setup links to more records.

#print &quot;$LimitNow <br>\n&quot;;
#print &quot;$PerPage <br>\n&quot;;
#print &quot;$MaxRecords <br>\n&quot;;
#print &quot;$Link <br>\n&quot;;

######################
### Setup LimitNow ###
my ($MaxOnPage);


my $TotalLinks;


###########################
### Calculate Prev Link ###
my $Prev;

my $QuitPrev = 0;
my $PrevStart = 0;
$PrevStart = $LimitNow;
#print &quot;$QuitPrev <br>\n&quot;;
my @Prev;
my $ShowBackLink = 1;
while ($QuitPrev == 0) {

# Subtract start amount from PerPage.
#print &quot;Prev Start -- $PrevStart - $LimitNow - $PerPage <br>\n\n&quot;;
$PrevStart = $PrevStart - $PerPage;
#print &quot;Prev Start -- $PrevStart < 0 <br>\n\n&quot;;

# Test if less than 0
if ($PrevStart < 0) {
#print &quot;No prev link \n\n&quot;;
$QuitPrev = 1;
}
else {
# Test if have added the back link yet
if ($ShowBackLink) {
$Prev = &quot;<a href=\&quot;$Link&$PrevStart&$PerPage\&quot;>Prev</a> &quot;;
$ShowBackLink = 0;
}

my $Showing = &ShowRecords($PrevStart, $PerPage, $MaxRecords);

my $Prev .= &quot;<a href=\&quot;$Link&$PrevStart&$PerPage\&quot; title=\&quot;Show records \[$Showing]\&quot;>%%TOTALLINKS</a>\n &quot;;
unshift (@Prev, $Prev);

#print &quot;Prev link = $Prev <br>\n\n&quot;;
}
}
for my $p (@Prev) {
$TotalLinks ++;

$p =~ s/%%TOTALLINKS/$TotalLinks/g;
#print &quot;$p <br>\n&quot;;
$Prev .= &quot;$p &quot;;
}


$TotalLinks ++;
my $Now = $TotalLinks;


###########################
### Calculate Next Link ###
my $Next;

my $QuitNext = 0;
my $NextStart = 0;
#print &quot;$QuitNext <br>\n&quot;;
my $FirstNext = 1;
my $NextInc = $LimitNow;
my $ShowNextLink = 1; # Variable flag to show next link and collect numbers
my $NextLinkStart; # Variable for starting next page
while ($QuitNext == 0) {


# Add start amount to PerPage.
#print &quot;$NextStart $LimitNow + $PerPage <br>\n\n&quot;;
$NextStart = $NextInc + $PerPage;

#print &quot;Next Start -- $NextStart $NextStart >= $MaxRecords <br>\n\n&quot;;

# Test if greater than Max Records.
if ($NextStart >= $MaxRecords) {
#print &quot;No next link <br>\n\n&quot;;
$QuitNext = 1;
}
else {
$TotalLinks ++;

# Set amounts for next link
if ($ShowNextLink) {
$NextLinkStart = $NextStart;
$ShowNextLink = 0;
}

my $Showing = &ShowRecords($NextStart, $PerPage, $MaxRecords);
#my $Showing = 20;
#print &quot;$Showing <br>\n&quot;;

$Next .= &quot; <a href=\&quot;$Link&$NextStart&$PerPage \&quot; title=\&quot;Show records \[$Showing]\&quot;>$TotalLinks</a>\n&quot;;

#print &quot;Next -- $Next <br>\n\n&quot;;
}

$NextInc += $PerPage;
}

# Add next link at the end
if ($NextLinkStart ne &quot;&quot; or $NextLinkStart != 0) {
$Next .= &quot; <a href=\&quot;$Link&$NextLinkStart&$PerPage\&quot;>Next</a>&quot;;
}

my $Test = &quot;$Prev $Now $Next&quot;;

return $Test;
}

#
# Show those records for that page
#
sub ShowRecords {
my $StartWith = shift;
my $PerPage = shift;
my $TotalRecords = shift;

#print &quot;$StartWith, $PerPage, $TotalRecords <br>\n&quot;;


my $StartRecord = $StartWith + 1;
my $EndRecord = $StartWith + $PerPage;
if ($EndRecord > $TotalRecords) { $EndRecord = $TotalRecords; }
my $Totals = &quot;$StartRecord-$EndRecord&quot;;

return $Totals;
}
 
You need to remove the '-C's from the sort. The -C operator acts on a filename,which is not what $a->[2] and $b->[2] are. In this case they are the last mod time. so:

my @sorted = sort { $b->[2] <=> $a->[2] } @unsorted;

If you really want to use Perl's filetest operators then you would do something like:

my @sorted = sort { -C $a <=> -C $b } readdir DIR;

and skip the stat()ing of the files. You will have to modify the rest of your code since @sorted is an array of strings now instead of an array of refs.

jaa
 
I tried your firest suggestion of removing the -C. That did not make any difference. I also tried your second suggestion but I don't know what you mean by &quot;skip the stat()ing&quot; and where/how to modiy the reast of the code. Remember, you're dealing with a non-rocket scientist. Thanks.
 
You say &quot;That did not make any difference.&quot;
What does the program do now? (ie. what does the output look like or what it is doing that you don't want it to do?)
AND what do you want it to do? (ie. What do you want to output to look like?)

Because when I take your sort code and stick it in a script, remove both -C's, and run it, it prints out the files in my directory sorted by modification time with the most recently modified first.

jaa
 
I have a directory with about 1200 images in it. No matter how I change the script (including your changes) the images come out in a random order. I cannot put a pattern to the output, but it is definately not in date order. I have also tried this script with about 10 files but 2 of them are dated last year. The script will display the ones from last year first, then the most recently modified.

I have tried the script with about 50 files (all dated this year) and it does seem to work. I am mainly having problems with the script when it is a large number of files.

Thanks for your help. I really do appreciate it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top