Solved

Urgent!  :)  Reading a Directory of Files

Posted on 1998-10-29
6
226 Views
Last Modified: 2010-03-04
Hello!

How do I impliment the following in my CGI program?

I've got a directory full of .htm files. (and only .htm files)
How do I make my program:
A. Show each one on the screen (all at the same time) in the order they were posted to the directory.
B. Make it possible to delete specific ones. (via a 'delete' button on the page the cgi creates.

Thanks a bunch!
Please hurry your answer.
-Mark
0
Comment
Question by:eversoslightly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 2

Expert Comment

by:shchuka
ID: 1205804
You should use the opendir function, something like this, for example:

#!/usr/bin/perl
print "Content-type: text/html\r\n\r\n";

opendir(DIR,"/rootdir/mydir");
while($file = <DIR>) {
    print "$file <form method=GET action=delete.pl?$file><input type=submit value='Delete'></form><br>\n";
}
closedir(DIR);

Also, you'll have another file, delete.pl, like this:

#!/usr/bin/perl
unlink $ARGV[0];

You might need to modify this a little to accomodate correct subdirectories and also incorporate error handlers.

Hope this helps,
-Aleks

0
 
LVL 1

Author Comment

by:eversoslightly
ID: 1205805
Thanks a lot - but something isn't working.

#!/usr/bin/perl
print "Content-type: text/html\r\n\r\n";

opendir(DIR,"/rootdir/mydir");
while($file = <DIR>) {
    print "$file <form method=GET action=delete.pl?$file><input type=submit value='Delete'></form><br>\n";
}
closedir(DIR);
  Here is the code you gave me for my one file... I modified the parts that needed to be modified, such as the path.
It appears that you need $file to equal something.... $file doesn't = <DIR> Or something - I'm not sure.... but when I call it, even though there are files in the directory, its not showing any of them when I call the program.
Can you help me any further?  Thanks a lot....

I'm going to have to reject this for now, so others are able to see the question.
0
 
LVL 1

Author Comment

by:eversoslightly
ID: 1205806
Thanks a lot - but something isn't working.

#!/usr/bin/perl
print "Content-type: text/html\r\n\r\n";

opendir(DIR,"/rootdir/mydir");
while($file = <DIR>) {
    print "$file <form method=GET action=delete.pl?$file><input type=submit value='Delete'></form><br>\n";
}
closedir(DIR);
  Here is the code you gave me for my one file... I modified the parts that needed to be modified, such as the path.
It appears that you need $file to equal something.... $file doesn't = <DIR> Or something - I'm not sure.... but when I call it, even though there are files in the directory, its not showing any of them when I call the program.
Can you help me any further?  Thanks a lot....

I'm going to have to reject this for now, so others are able to see the question.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:eversoslightly
ID: 1205807
Gerrr... I accidentally hit the button twice :)
0
 
LVL 2

Accepted Solution

by:
shchuka earned 50 total points
ID: 1205808
I'm sorry, I confused reading a directory with reading a file.  The correct line should be:

while($file = readdir(DIR)) {
    print ...
}

Hope this works,
-Aleks.
0
 
LVL 1

Author Comment

by:eversoslightly
ID: 1205809
Thanks a lot!

This really helped, and worked well.

-Mark
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question