Question:
Script to list files in a directory nicely?
Liz
2008-02-04 18:50:32 UTC
Hi all.
I have me an LAMP stack with apache 2.x and all the whistles and bells.

I have a department that produces PDF documents prolifically. They drop those documents in to a folder on a NAS which is then mounted to the web server and symbolically linked to /var/www/whatever. This is all set up and working as a poor mans publisher, but what you get when you log in that part of the site is the default apache index page. it lists the pdf docs, and you can link to them. As it should be. but UGLY, very ugly. Now, I am not looking for an entire content management system. Just a script, or a few lines of code that will allow me to list the files inside the directory in a
or something. Anybody. Help appreciated!
Three answers:
Chad L
2008-02-04 19:07:57 UTC
So, if you have a perl script, you can use the glob() function to get a list of files matching a pattern.



For instance:



my @pdf_files = glob("*.pdf");



Which you could then do something as simple as printing a header,and looping through the @pdf_files array like so:



print "Content-type:text/html\n\n";

print "...your html...";

my @pdf_files = glob("/var/www/whatever/*.pdf");

print "
    ";

    foreach my $file (@pdf_files) {

    print sprintf("
  • %s
  • ",$file,$file);

    }

    print "
";

print "...your html...";
Bob
2008-02-04 19:10:04 UTC
This script should read all of the files in a directory and stick each one in a div as a link and file name. You can pretty it up however you want.



Untested but it should work. You may also want to add a test to see if the file name ends in .pdf or whatever you want.




$dir = "/var/www/whatever/";

?>



// Open a known directory, and proceed to read its contents

if (is_dir($dir)) {

if ($dh = opendir($dir)) {



while (($file = readdir($dh)) !== false) {

?>

echo "" . $file . "\n";

?>

}

?>

closedir($dh);

}

}

?>
?
2016-12-04 12:49:04 UTC
look on the parameters for the "discover" command and you will see that it truly is going to manage almost each and all the standards you have. you will then could desire to deliver the output to the two awk or sed to get basically the itemizing names from the output which would be each and all the information which experience your seek standards.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...