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

 



perlkid
stranger

Feb 29, 2000, 2:33 AM

Post #1 of 11 (1257 views)
Directories. Can't Post

 
Hi Everyone,

Does anybody know how I can read a directory and put all of the names of the files into a form. After I do that I will also need to sort the contents of the form alphabeticaly. But any help would be very appreciated.

Thanks Everybody,

Good Night,

perlkid


japhy
Enthusiast

Feb 29, 2000, 4:29 AM

Post #2 of 11 (1257 views)
Re: Directories. [In reply to] Can't Post

Using the readdir() function:

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


opendir DIR, $dir or die "can't open $dir: $!";
if ($want_full_path_names) {
@files =
map { "$dir/$_" } # a full path name
sort # sort alphabetically
grep { -f "$dir/$_" } # only files
readdir DIR;
}
else {
@files =
sort # sort alphabetically
grep { -f "$dir/$_" } # only files
readdir DIR;
}
</pre><HR></BLOCKQUOTE>


perlkid
stranger

Feb 29, 2000, 11:41 AM

Post #3 of 11 (1257 views)
Re: Directories. [In reply to] Can't Post

 Thanks Japhy,

So now that all of the name of the files are in an array, how could I put them in a form?

Could I go like this,

<option value ="$array[0]">$array[0]

If that is close, would it put all of the file names in the form, or better yet would I do a foreach {} loop?

Thanks Japhy, I feel like I getting the hang of this stuff.

perlkid


[This message has been edited by perlkid (edited 02-29-2000).]


japhy
Enthusiast

Feb 29, 2000, 11:49 AM

Post #4 of 11 (1257 views)
Re: Directories. [In reply to] Can't Post

You'd want to use a for[each] loop:

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


for (@array) {
print qq!<option value="$_">$_\n!;
}
</pre><HR></BLOCKQUOTE>

Please note that ``for'' and ``foreach'' are synonyms in Perl.


perlkid
stranger

Mar 1, 2000, 2:12 AM

Post #5 of 11 (1257 views)
Re: Directories. [In reply to] Can't Post

 
Thanks again Japhy,

I didn't have a chance to try it during the day so I am going to go do it right now.

Thanks,

perlkid


perlkid
stranger

Mar 1, 2000, 2:45 AM

Post #6 of 11 (1257 views)
Re: Directories. [In reply to] Can't Post

 
Hi Japhy,

I just tried it out and it won't let me print any html under the readir code.

What am I doing wrong?

Thanks

perlkid


japhy
Enthusiast

Mar 1, 2000, 4:17 AM

Post #7 of 11 (1257 views)
Re: Directories. [In reply to] Can't Post

I'd have to see your code. I can't guarantee you copied it without a typo, etc.


perlkid
stranger

Mar 1, 2000, 11:30 AM

Post #8 of 11 (1257 views)
Re: Directories. [In reply to] Can't Post

 Here it is japhy, It's at the very bottom of the program because it wouldn't print html under it.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


opendir DIR, $dir or die "can't open $dir: $!";
if ($want_full_path_names)
{
@files = map { "$dir/$_" } # a full path name
sort # sort alphabetically
grep { -f "$dir/$_" } # only files
readdir DIR;
}
else
{ @files = sort # sort alphabetically
grep { -f "$dir/$_" } # only files
readdir DIR;
}
for (@files)
{
print qq!<option value="$_">$_\n!;
}
print "test";
</pre><HR></BLOCKQUOTE>

thanks

perlkid

[This message has been edited by perlkid (edited 03-01-2000).]


perlkid
stranger

Mar 1, 2000, 11:59 AM

Post #9 of 11 (1257 views)
Re: Directories. [In reply to] Can't Post

 Ok, I did something really silly,

Your code works great, I accidentally put the dir path as the url. He He He.

This is what I have now at the bottom of the program.

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


print qq(<p><select size="1" name="D1"> );
opendir (DIR, $dir) or die "can't open $dir: $!";
@files = map { "$dir/$_" } # a full path name
sort # sort alphabetically
grep { -f "$dir/$_" } # only files
readdir (DIR);
closedir (DIR);
@files =~ ;
for (@files)
{
print qq(<option value="$_">$_\n);
}
print qq(</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> );
exit;
</pre><HR></BLOCKQUOTE>

Now I am trying to make it so that it only will print the names of the files and not the whole path to the file and file name. Because in the drop down form it looks like this;

/path/to/image/dir/picture.gif

all the way down. But they are in alphabetical order, and that's good. just two more steps and it will be complete. The next is to get it so that there are file names only, and the to only print files with certain extention.

I tried using the s//g thing to get rid of the path before the picture but I never have used it before.

Thank you very much Japhy,

perlkid



[This message has been edited by perlkid (edited 03-01-2000).]


japhy
Enthusiast

Mar 1, 2000, 12:15 PM

Post #10 of 11 (1257 views)
Re: Directories. [In reply to] Can't Post

Ah. You can't use a pattern match on an array like you're doing. And if you don't want the full path names in the array, then don't use the portion of the code that does that.

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


if ($want_full_path_names) {
@files =
map { "$dir/$_" } # a full path name
sort # sort alphabetically
grep { -f "$dir/$_" } # only files
readdir DIR;
}
else {
@files =
sort # sort alphabetically
grep { -f "$dir/$_" } # only files
readdir DIR;
}
</pre><HR></BLOCKQUOTE>

If you use that code AS IS, if you don't have the $want_full_path_names variable set, it won't store the full path name.


perlkid
stranger

Mar 2, 2000, 1:53 AM

Post #11 of 11 (1257 views)
Re: Directories. [In reply to] Can't Post

 
Ok,

I tried that and it worked out great.

Thanks, How could I store files in the array only with a certain extention, like .db.

Thanks Japhy,

perlkid

 
 


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

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