Solved

Urgent!  :)  Reading a Directory of Files

Posted on 1998-10-29
6
184 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now