in reply to Find, read, write out contents of a certain file type recursively...

Thanks for the help guys. I got this thing working. I will post my modified code. Since I am new, I will look around and see, if I can post the source so someone else can benefit. Thanks!
# Variables $output = "/home/web/path/to/foo/foo.htm"; $jscriptdir = "/home/web/foo/javascript/"; $javadir = "/home/web/foo/servlet/"; $cgidir = "/home/web/cgi-bin/"; # Open the output file open(NEW,">$output") or die "Can't Open $output: $!"; print NEW "<html><head><title>Comment Extractor</title></head>\n"; print NEW "<body>\n"; print NEW "<h1>Comment Extractor</h1>\n"; print NEW "<hr>\n"; # Let's find files. Call jsfind for the dirty work find (\&jsfind, $jscriptdir); print NEW "</body></html>\n"; close NEW; exit; sub jsfind { # get all file names that have last 2 characters "js" if($File::Find::name=~/\.js$/) { # ignore js files in those stupid Frontpage _vti* directories if ($File::Find::name=~/\_vti/) {} else { # get name of script file and print it in red $script_title = $_; print NEW "<b><font style=\"color:red; size:16px; text-tra +nsform: uppercase\">" . $script_title . "</font></b>"; # placeholder for file # start opening files to read open(FILE, $File::Find::name) or die "could not open f +ile - $_ - : $!"; # iterate thru files to find match //** while (<FILE>) { if ($_ =~ /\*\*/) { # print files to $output print NEW substr($_,4) . "<br>"; } } close FILE; } } else { return; } }
  • Comment on Re: Find, read, write out contents of a certain file type recursively...
  • Download Code

Replies are listed 'Best First'.
Re: Re: Find, read, write out contents of a certain file type recursively...
by graff (Chancellor) on Nov 15, 2002 at 05:13 UTC
    I will post my modified code

    Thanks! (Um, be sure to post the whole thing... -- I didn't see a line saying "use File::Find" when I read this post.)

    Just one concept you should consider: doing a recursive directory search for files of a given type (e.g. *.js) is a very common facility that is handy for a wide range of particular needs -- that's why the GNU "find" utility (and the decades-old unix "find" that it's modeled on) is such a basic, essential component on so many systems (it's been ported to windows, etc).

    Even if you want to stick with File::Find (which happens to be a few times slower than GNU "find"), you should consider making it a separate utility by itself, and keep just the editing function in a simpler app that works on a single file, or on a list of files read from stdin -- e.g.:

    my_find_utility '*.js' | my_comment_extractor
    This way, when you come up with some other particular edit or summarization process to be done on all files of a given type in a directory tree, you don't have to re-write the part that recurses through the directories. Just write a script that will apply the new process to any list of file names on stdin, and use the same front-end program (or GNU "find") to feed it.