Scripts to list Directory file & contents

Posted on 1999-01-24
Last Modified: 2010-05-18
I'm looking for perl script to list the files in a directory and the contents of the files. The Contents could be driven from another file with single line discriptsion.
 If you know of something like this please advise.
Question by:dpdr
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
  • 2
  • 2
LVL 84

Expert Comment

ID: 1210282
opendir(DIR, $some_dir) or die "can't opendir $some_dir: $!";
while( defined($_=readdir(DIR)) ){
    print "$_\n";
#I'm not sure I understand your second sentence.

Expert Comment

ID: 1210283
could use @file_stats = stat $file;
if you are talking about file att's such as size, mod date etc.

( $dev, $ino, $nlink, $uid, $gid, $rdev, $size,
  $atime, $mtime, $ctime, $blksize, $blocks ) = stat $filename


Author Comment

ID: 1210284
I guess I should have been a little clearer. I'm tryint to creat  a listing of documents in a directory. That have the document numbers and a comment line 25 chars. The output of the listing would be HTML and built on the fly. So once the directory was built files added to it would be pickup when someone would access it. then an edit script could be wriiten to add information to a file with the comment data in it.

Accepted Solution

tpryor earned 400 total points
ID: 1210285
Got ya,

we do this at work a lot.

For speed we index the directory first into a DBM or flat text file usally nightly but we can maually execute it also.

Then we have a front end script that reads the index file and reports the results to the screen.

First decide on what tags you will use and load them into your files.

# next lets read the directory

opendir( DIR, "$dir_path" );

# open every file in the dir

foreach $file ( readdir DIR ) {
      next if ( $file =~ /^\./ ); # skip the . .. files on unix
      open( FILE, "$dir_path:$file" ) || die "couldn't open $file: $!";

# look through each file for our tags
      while ( $line = <FILE> ) {
            last if ( $line =~ /^<!---End of Tags---->/ );
# tags look like this <!--name=value-->
            if ( $line =~ /^<!--/ ) {
                  $line =~ /^<!--(.*[^\=])=(.*[^\-])-->/;

# store tags in array to print to index file later
                  push( @{$tags{"$file"}},  [$1, $2] );
close FILE

# now lets format our tags for index file

foreach $file ( sort keys %tags ) {

       $index .= "$file|";
        foreach $array ( @{$tags{"$file"}} ) {

# we will store our values only in the $index scalar

               $index .= "@$array[1]|";
   $index .= "\n";
closedir DIR;

# now we will write our tags to a flat index file

open ( INDEXFILE, ">$path_to_index_file" );
print INDEXFILE $index;

########### end #############

the file will look like this


if we had three html files with these tags


NEXT you would have a script read the index file and output the results to html.

it is faster and easier to deal with

######### display code ##########

open ( INDEX, "$path_to_index_file" );

# read one record at a time
while( $record = <INDEX> ) {
# split the record up into its parts
@fields = split( /\|/, $record );
# print out with your html
print join( ",", @fields) . "<br>";
close INDEX;
# thats it

########## end ##########

hope that helps


Author Comment

ID: 1210286
Thanks that looks like What I need.
I got it to do some stuff in ksh but perl should be faster.

From what you wrote I should be able to modify it if needed.
But starting in perl would mean learning it.


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

Suggested Solutions

Title # Comments Views Activity
Port 80 requests 16 111
pipe to sed or perl, please help 5 144
Edit Odbc.ini using perl module 2 96
perl syntax 3 55
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…
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…
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…

739 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