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!

searching directory/files. 2

Status
Not open for further replies.

perlone

Programmer
May 20, 2001
438
US
Hello. I need to search to see if the username is taken or not. I made a directory for each users and each directory has a file called stats.cgi and contains the user info. To do that, I tried the following:


opendir(DIR, "$basedir");
@members = readdir (DIR);
closedir(DIR);

foreach $member (@members) {
next if (($member eq '.') || ($member eq '..'));
open(F, "$basedir/$member/stats.cgi");
@user = <F>;
close(F);
}

foreach $user (@user) {
chomp($user);
$username = $user[0];
}

if ($FORM{'username'} eq &quot;$username&quot;) {
print &quot;taken!&quot;;
}else{
print &quot;done&quot;;
}



The code above should search for the username in each directory. Yes, it's a hard way to see if the username is taken or not but i'm adding more info and so it'll be a little easier to me. I don't know why but it's only searching for one directory. It's not searching inside all the directory to see if it's taken or not. Am i missing something in the code? Thanks again for your time and hope you could help.

-Aaron
 
I think your code nesting is a bit goofed up.

By the time you get to your if statement $username
has the first line of the last stats.cgi file in it.

You need something like this I think...
Code:
opendir(DIR, &quot;$basedir&quot;);
@members = readdir (DIR);
closedir(DIR);

$result = &quot;done&quot;;
USERSEARCH: foreach $member (@members) {
   next if (($member eq '.') || ($member eq '..'));
   next unless open(F, &quot;<$basedir/$member/stats.cgi&quot;);
   @user = <F>;
   close(F);

   foreach $user (@user) {
      chomp($user);
      if ($FORM{'username'} eq $user) {
         $result = &quot;taken!&quot;;
         last USERSEARCH;
      }
   }
}
print $result;


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top