in reply to file wildcards in Win32

Ok, not the cleanest code by any stretch, but it works.
usage: greplike.pl C:\ .c /*
will then look through all Source code on C for the beginnings of comments.
One of the little features that I like is that if you have a file name with more than one
"." in it ( ".htm.txt") it will grab only the last part after the extension for examination.
Could be made better so it searchs across multiple lines, recognizes and strips "" from the string
you are searching for unless you escape them, and so on. Fun little program to make.

Update
Corrected my two errors that Ovid pointed out.
Thanks Ovid! :)
use File::Find; use strict; use integer; my $filecounter = 0; my $dir = @ARGV[0]; @ARGV[1] =~ m/(.*)(\.){1}(\w+)$/g; my $filename = $1; my $filesoftype = $3; my $stringtosearchfor = @ARGV[2]; print "Looking for files of type $filesoftype in $dir containing $stri +ngtosearchfor\n"; sleep 10; find(\&lookingfor, $dir); print "Found $filecounter files of type $filesoftype!\n"; sub lookingfor() { if (($_ =~ m/\.+($filesoftype)+$/io) && (-f $_)) { $filecounter++; open INPUT, "<$_" || die "Unable to open $_ for examination: $ +!\n"; while(my $line = <INPUT>) { chomp $line; if ($line =~ m/$stringtosearchfor/gio) { print "Found match in $File::Find::name at line $.\.\n +"; } } close INPUT; } }

Replies are listed 'Best First'.
RE: Re: file wildcards in Win32
by Ovid (Cardinal) on Jun 16, 2000 at 23:32 UTC
    You can skip the $linecounter variable and use "$." (that's a dollar sign followed by a dot). The "dollar dot" variable keeps track of the current input line number.

    Also, don't forget to use the /o switch in a regex included in a loop if the regex never changes. This prevents the regex compiler from recompiling the regex every time time it encounters it and will improve program performance.

    Nice code, btw.

      Two excellent points! I had forgotten about $. and I had used /o in the first regex
      but not in the second. You can tell it's Friday and my mind is elsewhere. Thanks for the insight.