?
Solved

Urgent!  :)  Reading a Directory of Files

Posted on 1998-10-29
6
Medium Priority
?
232 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Six Sigma Control Plans
Suggested Courses

770 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