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: Perl Programming Help: Beginner:
sarching through a directory, and displaying...

 



ultimategamer2k
User

Feb 25, 2001, 6:01 PM

Post #1 of 4 (492 views)
sarching through a directory, and displaying... Can't Post

whats the code to search through a directory, and display all the files that start with say "a" or say all the files that start with the letter a-m
someone please help me!!!!

visit my web site at http://ultimategamer.cjb.net


sleuth
Enthusiast

Feb 25, 2001, 6:12 PM

Post #2 of 4 (491 views)
Re: sarching through a directory, and displaying... [In reply to] Can't Post

 
Funny that I happend to have a file open that does just that, I think a super powerfull being sent me to help you.

Anyway, here's how


Code
$dir = "/path/to/dir"; 
opendir (DIR, $dir) || die "can't open $dir: $!";
@files =
sort
grep { -f "$dir1/$_" }
readdir (DIR);
closedir (DIR);
foreach $file (@files)
{
if (substr($file, 0, 1) =~ /[A-Ma-m]/){
print "$file";
# all these files start with a letter between a & m
}
}

I'm sure you could swing that do do what ever you want,

Tony



ultimategamer2k
User

Feb 25, 2001, 6:46 PM

Post #3 of 4 (489 views)
Re: sarching through a directory, and displaying... [In reply to] Can't Post

that doesnt seem to work... i modded it a bit.. heres what it looked like

#!/usr/bin/perl
########################################################################################################
############################### DONT TOUCH #############################################################
########################################################################################################
use CGI qw(:standard); #
$gameName = param('game'); #
$console = param('cns'); #
$action = param('action'); #
$search = param('search'); #
$start = param('start'); #
$end = param('end'); #
print "Content-type: text/html\n\n"; #
print qq(<body bgcolor="#000000" text="#FFFFFF" link="#C0C0C0" vlink="#C0C0C0" alink="#FFFFFF">); #
########################################################################################################
################################# END DONT TOUCH #######################################################
########################################################################################################


if ($action eq "files"){
open (FILE, "$console/".$gameName.".ug") or die "Couldn't open: $!";
@game = <FILE>;
close FILE;
foreach $codes (@game) {
($description, $code) = split(/:/, $codes);
print qq(<table border="0" cellpadding="0" cellspacing="0" width="38%"><tr>
<td width="47%" valign="top">$description</td>
<td width="53%" valign="middle">$code</td></tr></table>);
}
}
elsif ($action eq "search"){
$dir = $console;
opendir (DIR, $dir) || die "can't open $dir: $!";
@files =
sort
grep { -f "$dir1/$_" }
readdir (DIR);
closedir (DIR);
foreach $file (@files)
{
if (substr($file, 0, 1) =~ /[$start=$end]/){
print "$file";
# all these files start with a letter between a & m
}
}

}


visit my web site at http://ultimategamer.cjb.net


sleuth
Enthusiast

Feb 26, 2001, 2:26 AM

Post #4 of 4 (482 views)
Re: sarching through a directory, and displaying... [In reply to] Can't Post

 
It doesn't SEEM to work implies that you aren't getting any errors but aren't getting any results. So I'll go with that.

I spotted this code

$start=$end

It should be $start-$end

No '='

Other than that, if you can't get it to work, make sure that

the $console var checks out ok, as in it's an existing directory with 777 permissions.

Also make sure $start and $end exist too. It's probably more likely that if you make the minor code modifications and check the permission of the directory which you are searching it will come through for you.

Remember, chmod 777 on that directory.

Tony


 
 


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

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