Solved

Urgent!  :)  Reading a Directory of Files

Posted on 1998-10-29
6
199 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 138
Check file date before getting folder path 1 64
problem with using the glob function 1 64
perl: Cleaning meta tags using RegEX 12 64
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

23 Experts available now in Live!

Get 1:1 Help Now