Paul,
I did not rewrite any windows program but just thought of coding such a
program.
That code which you re-wrote was good actually, except for the
backslash which you missed , as in the following:
find(\&Wanted, '\/');
I will be re-writing the same code without the File::Find module. I am
stuck in the algorithm actually but before I seek help on this, I would
like to try once more.
Thanks for your help!
Cheers,
Gunwant Singh.
Paul Lalli wrote:
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/