CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
File Count

 



Gathus
Deleted

May 18, 2000, 4:09 AM

Post #1 of 6 (1193 views)
File Count Can't Post

Hi,
I'm looking for a script that will count the files in a specific directory, and display the results through an ssi tag, so the results would look something like this:

Gallery 1 (10 images)

Does something like this exist?

Thanks

T.


hamed53
Deleted

May 19, 2000, 9:59 AM

Post #2 of 6 (1193 views)
Re: File Count [In reply to] Can't Post

i can write it for u, but are u willing to pay?


monocle
User

May 19, 2000, 3:54 PM

Post #3 of 6 (1193 views)
Re: File Count [In reply to] Can't Post

This could be made quicker if you just get the number of elements in the @linez array, but this will allow you to only count a certain file type (i.e. gif or jpg)

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/bin/perl

# Get the name of directory to count
$dir_to_count = "$ENV{'QUERY_STRING'}";

# Read contents of your directory
opendir(RID,"$dir_to_count") | | print "cant $!";

flock(2, RID);
@linez = grep(/./,readdir(RID));
flock(8, RID);
close(RID);

# Initialize various extention counters
$html = "0";
$txt = "0";
$gif = "0";
$jpg = "0";
$sound = "0";
$direcs = "0";
$misc = "0";

# Look at each element of the directory and count them by f1le type
foreach $struck (@linez) {
if (!(-d $struck)) {
if ($struck =~ /\.htm$/i) {
$html++;
}
elsif ($struck =~ /\.html$/i) {
$html++;
}
elsif ($struck =~ /\.txt$/i) {
$txt++;
}
elsif ($struck =~ /\.gif$/i) {
$gif++;
}
elsif ($struck =~ /\.jpg$/i) {
$jpg++;
}
elsif ($struck =~ /\.wav$/i) {
$sound++;
}
elsif ($struck =~ /\.au$/i) {
$sound++;
}
else {
$misc++;
}
}
}

# Add all the file types up or not --- this is up to you
$file_total = ($html + $txt + $gif + $jpg + $sound);

print "Content-type: text/html\n\n";
print $file_total;
</pre><HR></BLOCKQUOTE>

call it like this:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


<html>
<body>

Gallery 1 (<!--#include virtual="/cgi-bin/count_files.cgi?path_and_name_of_directory_to_count" --> images)

</body>
</html>
</pre><HR></BLOCKQUOTE>

I just tested this and it should will work.

Monocle

BTW - This is free of charge - If you feel like it was worth it, you can thank me by giveing my music a good listen.
http://www.mp3.com/monocle

[This message has been edited by monocle (edited 05-19-2000).]


Gathus
Deleted

May 19, 2000, 7:16 PM

Post #4 of 6 (1193 views)
Re: File Count [In reply to] Can't Post

monocle,
Thanks for the script. I have it almost working, but it's counting strangely. There's only 3 images in the directory at the moment and it's showing on the shtml page as 00123. I really only need it to count jpg files since that's all that's going to be in the gallery folders. Can you tell me what part of the script needs to be adjusted to do this? Also how to get the acurate count :-)

Thanks

T.


TheGame+
Deleted

May 20, 2000, 3:11 AM

Post #5 of 6 (1193 views)
Re: File Count [In reply to] Can't Post

Here's a slight adaptation of the previous script - this should only count the files ending with '.jpg' :

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl
# Get the name of directory to count
$dir_to_count = "$ENV{'QUERY_STRING'}";
# Read contents of your directory
opendir(RID,"$dir_to_count") &#0124; &#0124; print "cant $!";
# Decide which files to count (only .jpg here)
@linez = grep(/\.jpg$/,readdir(RID));
close(RID);
$file_total = scalar @linez;
print "Content-type: text/html\n\n";
print $file_total;
</pre><HR></BLOCKQUOTE>


Gathus
Deleted

May 20, 2000, 4:32 AM

Post #6 of 6 (1193 views)
Re: File Count [In reply to] Can't Post

TheGame,

It worked perfectly! Now just one last question. I swear it'll be the last :-)

If I wanted at a later date to add the ability for the script to recognize a .gif file, and have those two counts (.jpg and .gif) combined, how would I do that. I know that sounds stupid with the script being so small, but I'm clueless when it comes to doing real mods on a perl script.

Thanks again.

T.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives