Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Scripts to list Directory file & contents

Posted on 1999-01-24
5
Medium Priority
?
203 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 85

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 1600 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

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

926 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