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 Directories....

Status
Not open for further replies.

perlone

Programmer
May 20, 2001
438
US
Hi,

I'm creating a form and it will save it in to a text based database. Here's what I have:

First, user will fill out the form.
Second, after user filled out the form, a new directory will create with the email address that user provided. Let's say the user's email is john@mail.com, then the directory's name is john@mail.com. In that directory, there's 5 text files. One called user.txt which included user's username.

Problem:
Let's say another user filled out the form but I don't know how to search the directory to find to see if the username is taken or not. Anyone have any idea?
 
A word of advice about letting users create stuff on your server.

I would be highly against this. Allowing people to create directories on your server can cause a catastrophe.

Anyways, to answer your question, the easiest way that I would do this would be to try to open the directory that user specifies (ie, his email address). If you can't open the directory, then it doesn't exist and allow the user to continue, if not, then tell him that a user with that username (or whatever) is taken and that he needs to choose another one.

Here is some sample code:

[tt]

print "what is the directory?\n";
chomp($mydir = <STDIN>);

$createnew = checkdir($mydir);
if ($createnew) { print &quot;choose another username&quot;; }
else { print &quot;congrats, you are now a member&quot;; }

sub checkdir {
my $dir = shift;
$diropen = opendir(DIR, &quot;$dir&quot;);
if (!$diropen) {
return 0; # directory does not exist, user can create it
} else {
return 1; # directory exists, tell the user to pick another name
}
}
[/tt]

Hope this helps.

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
The easiest way to check if a directory exists is to use the -d function:
Code:
if ( -d &quot;the/directory/name&quot; ) {
   print &quot;The directory exists\n&quot;;
} else {
   print &quot;The directory does not exist\n&quot;;
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Yes, I usually use perl's -e to find out wheather the email exit or not. Server actually check to see if it exist or not. If it's not, a directory called test@host.com will be made along with 5 text files. One text file is called user.txt which stores test@host.com's username. I have the script to check to see wheather if the email is exit or not but I want to open all the directory in $root/cgi-bin/users and I want to search every user.txt to see if the username is taken or not. Thanks for your time.
 
It would be more efficient to store all the usernames in a single file and search that file than to search all the subdirectories and open a file in each and read it to see if it contains the username. That can be done using a dirhandle, but it's VERY inefficient.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
To tell you the truth, it doesn't sound like this is an operation you want to conduct through the filesystem at all. Can you use a relational database instead?

The reason is that all filesystem operations are extremely time consuming and if two or more people run this program simultaneously, you may run into file locking difficulties.

If you don't have access to a relational database, try to use the Berkeley DB structure. You can find the modules at
My two cents.

And now I'm broke. brendanc@icehouse.net
 
Good advice, sophisticate. The only reason I didn't suggest it that is so many people have ISPs that don't have either MySQL or Berkeley DB, and won't let anyone install it. My preference is MySQL - it's terrific.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thanks for everyone's reples. I'll try to use the relational database DB. But can you guys telll me a truth? Is it hard or easier than MySQL? The bad part is like tsdragon said most of the hosting service don't support either. But i'll give it a try...
 
Berkeley DB is actually pretty easy to use. It basically reads your DB into a hash. It's not nearly as powerful or flexible as MySQL, but it works well for simple applications. I like MySQL for the power and flexibility, and the fact that a lot of the work of generating reports (selection, sorting, grouping) is done by MySQL and the query you build, rather than in perl code, so it simplifies programming. The hardest part about MySQL is learning how to use SQL, but it's not terribly complicated.

Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top