On Jun 19, 12:16 pm, [EMAIL PROTECTED] (Gunwant Singh) wrote:
> Hi all,
>
> I wrote a code that can search any file/folder on a PC given that you
> must give the file name with its extension.
> The code works fine. Any suggestions to make the code better or faster.
> Here is the code:
> ---------------------------------------------------------------------------­-----------------
> use strict;
> use warnings;
> use File::Find;
>
> chdir('/');
> print "Enter the name of file/folder to search:\n";
> my $fn=<STDIN>;
> chomp($fn);
> my $dir="\\";
> find(\&Wanted,$dir);
>
> sub Wanted
> {
> if (($_ eq '.')||($_ eq '..')) { } else
> {
> if ($_ eq $fn)
>     {
>     my $cwd = Win32::GetCwd();
>     print "\nFile found in $cwd";
>     }}
> }

So, basically, you wrote C<find> (or `dir /s`)?  Okay....

For one, you shouldn't assume that the user is using a particular
operating system.

That first line of the Wanted() subroutine is doing absolutely nothing
of value.

The chdir('/') line is also doing nothing.

Fild::Find automatically stores the name of the directory it's
currently recursing through in $File::Find::dir, so there's no need to
call an external function to get it (least of all an external function
that not every user will have access to)

Even in Windows, you can (and should) write directories using front
slashes, not double-backslashes.  It is only the cmd shell that
insists you use \ instead of /


So I would take your program and rewrite it like this:

#!/usr/bin/perl
use strict;
use warnings;
use File::Find;

print "Enter the name of file/folder to search:\n";
chomp(my $fn=<STDIN>);
find(\&Wanted, '/');

sub Wanted
{
   if ($_ eq $fn)
   {
      print "File '$fn' found in $File::Find::dir\n";
   }
}
__END__


Hope that helps,
Paul Lalli


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to