Solved

Scripts to list Directory file & contents

Posted on 1999-01-24
5
188 Views
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.
0
Comment
Question by:dpdr
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
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.
0
 
LVL 2

Expert Comment

by:tpryor
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
t

0
 

Author Comment

by:dpdr
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.
0
 
LVL 2

Accepted Solution

by:
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;
close INDEXFILE

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

the file will look like this

first|Mickey|Mouse|555-1212|
second|Donald|Duck|555-1313|
third|Bugs|Bunny|555-1414|

if we had three html files with these tags

<!--FName=Mickey-->
<!--LName=Mouse-->
<!--Phone=555-1212-->

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

t
0
 

Author Comment

by:dpdr
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.

Thanks
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

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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

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

25 Experts available now in Live!

Get 1:1 Help Now