Quick file search
sh1tn
created: 2006-01-04 12:45:27
In case when I don't have time to wait for the Windows
"quick" (even with Indexing service started) search program
and SQL engine is too much efford, here is my solution:
# my 'music' directory contains over 5000 files
# so the fastest way to be listed:

D:\tree /f /a .\music > \music_list.txt

# later the following script is 'move'd to
#  %systemroot%\system32

### begin
use strict;

my $argv  = join '.*', @ARGV;
my $song  = qr/\|(?!\+\S{3})(\s.*?)$argv/i;
my $clean = qr/^\W+(.+?)\W+$/;
my $file  = 'd:\music_list.txt';
my $album = q::;

open my $fh, $file
	or die "cannot open $file: $!$/";

while(<$fh>){
	/(\\|\+)-{3}/ 
	   	and $album = $_
	   	and next;
	if (/$song/) {
          s/$clean/$1/ for $_, $album;
          $album 
             ? print "album: $album$/","song:  $_$/$/"
             : print "album: .     $/","song:  $_$/$/";
          $main::counter++;
	}
}

print "$/total: $main::counter song(s) found$/"
	if $main::counter;

__END__
### end

# finally 
D:\copy ffind.pl %systemroot%\system32
D:\ffind.pl compare
album: Good music
song:  Sinead O'Connor - Nothing Compares 2 U.mp3

album: party
song:  Sinead O'Conner - Nothing Compares To You.mp3


total: 2 song(s) found
That's all.


Re: Quick file search
created: 2006-01-04 22:59:51
It would be interesting to see how much faster the win/dos "tree" utility is than the [cpan://File::Find] module (or any of its relatives on CPAN). I know that in unix, File::Find takes much longer than the standard "find" utility for a directory tree of any considerable size.

The only problem with the OP code is that you have to remember to run "tree" yourself -- and save its output in a specific file whose name is hard-coded in your perl script -- before running the script, so that you know your file list is up-to-date. But that's unnecessary -- just run "tree" inside the perl script (it'll still run just as fast):

my $music_path = ".\\music";  # probably should make this an absolute path...

open my $fh, "D:\\tree /f /a $music_path |"
    or die "cannot run tree on $music_path: $!";

while ( <$fh> ) {
   # everything else stays the same...
}
(not tested -- I'm not a windows user)

Actually, it might also be interesting to see whether a win/dos port of the unix "find" utility is slower or faster than "tree"...

Re^2: Quick file search
created: 2006-01-05 00:36:14
The lack of unix "find" utility is enormous but "tree" filtered results solve to some extent this problem:
# rough comparison
D:\music>perl -MFile::Find -e "find(sub{$File::Find::name}, '.');print time-$^T"
6

D:\music>perl -e "`tree /f /a`;print time-$^T"
1
Where "music" contains 5547 files (without the directory inodes).


Re^3: Quick file search
created: 2006-01-05 00:46:57
The lack of unix "find" utility is enormous

I'm wondering what you mean by that. There are windows ports of "find" available (google "unix tools for windows"); the ATT Research Labs version and the cygwin version are both authentically "unix-like" (or maybe "gnu-ish" is the better term).

"tree" filtered results solve to some extent this problem

Oh yes. That seems consistent with what I've seen in the unix domain -- about a 5- or 6-to-1 speed ratio comparing File::Find to "find". For really big directory trees, that multiplier becomes devastatingly significant.

Re^4: Quick file search
created: 2006-01-05 07:26:28
One thing to note is that you need to put the Cygwin before Windows in the PATH for this to work, since there already is a "find" (although it's really a rather weak "grep") on the system.

/J

Re^4: Quick file search
created: 2006-01-06 08:11:17
No need for tool from the "outside" world, maybe just:
my $search_what  = shift 
	|| die "no search criteria provided\n";
my $search_where = shift;
my $file  = qr/\|(?!\+\S{3})(\s.*?)$argv/i;
my $clean = qr/^\W+(.+?)\W+$/;

$dir = -d $dir 
	? $dir 
	: '.';

for(`tree /a /f $search_where`){
	/(\\|\+)-{3}/ 
	   	and $dir= $_
	   	and next;
	if (/$search_what/) {
		s/$clean/$1/ for $_, $dir;
	   	$dir 
	   		? print "dir:   $dir$/","file:  $_$/$/"
	   		: print "dir: . $/","file:  $_$/$/";	   	
	}
}


Re^2: Quick file search
created: 2006-01-05 06:46:31
I know that in unix, File::Find takes much longer than the standard "find" utility for a directory tree of any considerable size.

This was discussed in considerable length in Myth busted: Shell isn't always faster than Perl and was wondering if you can back up the claim of "much longer" . My tests show differences of only a fraction of a second on 100 meg tree.


I'm not really a human, but I play one on earth. flash japh
Re^3: Quick file search
created: 2006-01-05 23:48:21
The evidence I've gathered about unix "find" vs. File::Find is at least a couple years old, and I haven't checked to see whether there has been an update to File::Find since then, nor tested it more recently to see whether it has improved within the last couple years. (I assume unix "find" remains about the same.)

In any case, I posted some benchmarks here and here. Looking at those again just now, my statement above about "5- or 6-to-1" should have simply been "5-to-1".

perlmonks.org content © perlmonks.org and graff, jplindstrom, sh1tn, zentara

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03