in reply to Find filename that has the pattern in a directory
The above gets only the file names, if you want the full path, then my @files2 = map{"C:/temp/".$_}grep{/\.pl$/}readdir DIR; The glob operator can be used also albeit there are pitfalls as there are several different incompatible glob()'s out there.opendir (DIR, "C:/temp") or die "unable to open C:/temp $!\n"; my @files2 = grep{/\.pl$/}readdir DIR; print join("\n",@files2),"\n";
update: if you want all the files in a directory, thenmy @files = glob("C:/temp/*.pl"); print join("\n",@files),"\n"; #full paths
readdir returns all files. Directories are a special type of file. the files . and .. are files. The -f file test in the grep will filter the output down to all "normal" files (no directories or . and .. entries).opendir (DIR, "/root") or die "unable to open /root $!\n"; @all = grep{-f}readdir DIR;
Update: Well I think I mis-understood the question. I guess the question should have been:
I want an efficient way to search through a directory and get a list of files, each of which contain a certain pattern. .
I think using command line grep is just fine. What about that solution is not "fast enough" or "efficient enough" for the application? Are you having trouble capturing and using the output of the command line grep command? Is this a Windows box and you don't have grep. Please explain the problem further.
Every file will have to be opened and read sequentially until either the desired pattern is found or EOF is reached - that is true no matter what you do. A Perl program can use a different regex syntax than the command line grep, but that was not even part of the question.
|
|---|