Solved

Scripts to list Directory file & contents

Posted on 1999-01-24
5
185 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
perl to mysql 5 127
HTTP Error 502.2 - Bad Gateway 3 194
Perl efficient DB Call 8 75
Extract data from span tag 1 87
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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

7 Experts available now in Live!

Get 1:1 Help Now