title: Read files searching for a string and print its path
body: <p>I am trying to write a script in Perl that searches in a particular directory and all the subdirectories. The objective for this is that the script has to read all the files in the directory and all subdirectories looking for a particular text string (any string I define). If the string is found in the file then the script prints the path and name of the file in a new text file, and continues with all the files in the directory tree.</p>
  
  <p>I have somenthing like this, but I am not sure on how to continue. I am a beginner with Perl, and have no clue on all the options of this.</p>
  
  <pre><code>#!/usr/bin/perl
  use strict;
  use File::Find;
  
  my $dir = 'C:\PATH\TO\DIR';
  my $string = "defined";
  
  find(\&amp;printFile, $dir);
  sub printFile {
     my $element = $_;
     open FILE, "+&gt;&gt;Results.txt";
     if(-f $elemento &amp;&amp; $elemento =~ /\.txt$/) {
         my $boolean = 0;
         open CFILE, $elemento;
         while(&lt;CFILE&gt;) {  
             if ($string) {
                 print FILE "$File::Find::name\n"; 
             }
             close CFILE;
        }
     }
     close FILE;
  }
  
  sleep(5);
  </code></pre>