orthanc has asked for the wisdom of the Perl Monks concerning the following question:
hey ho ppl
This question is really about deleting a file from a directory containing a few thousand files.
Lets begin... I need to unlink 1 file but the only data I have is the id which starts the filename. Thus I need to use filename globbing, which on a few thousand files takes some time.
opendir(DIR,"/path") || die("No opendir on $path\n"); my @files = grep { /^${id}\_.*$/ } readdir DIR; closedir(DIR); my $filename = "/path/$files[0]"; unlink("$filename") || die("No unlink on $filename\n");
Please excuse the shoddy code and lack of error checking, but its only an example.
Rather than having to grep the whole dir everytime I want to delete a file, is there a simpler and more importantly faster way that I'm for some reason blind to?
Thanks for any help
Orthanc
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: removing a needle from a haystack
by jeroenes (Priest) on Feb 14, 2001 at 20:37 UTC | |
Re: removing a needle from a haystack
by McD (Chaplain) on Feb 14, 2001 at 20:49 UTC | |
Re: removing a needle from a haystack
by TheoPetersen (Priest) on Feb 14, 2001 at 21:29 UTC | |
Re: removing a needle from a haystack
by Pahrohfit (Sexton) on Feb 14, 2001 at 22:37 UTC | |
Re: removing a needle from a haystack
by binary* (Novice) on Feb 14, 2001 at 22:39 UTC | |
Re: removing a needle from a haystack
by Anonymous Monk on Feb 15, 2001 at 03:07 UTC |