Invalid top directory at d:/perl/lib/file/find.pm line 562

Invalid top directory at d:/perl/lib/file/find.pm line 562

am 27.09.2007 11:02:29 von ninad.29

Hi, i am using the following script to find a file entered by a user.

use Win32::DriveInfo;
use File::Find;

my @drives = Win32::DriveInfo::DrivesInUse(); #Get the drives on a
Machine

my $cnt=@drives; #Number of Drives

my $i=$cnt; #Counter assignment

#The following loop appends "/" to the drive letter #if the drive is
a
fixed drive.

for($i;$i>0;$i--)
{
$type=Win32::DriveInfo::DriveType($drives[$i-1]);
if($type==3)#fixed drives
{
$d[$i-1]=$drives[$i-1].":"."\\","\\";
}
}

$temp=$ARGV[0]; #store filename to be found

my @t=split(/\./,$temp); #Split the filename to get the extension

my $cnt=@t;

$f=".".$t[$cnt-1]; #Append "." and extension

find(\&cleanup,@d); #standard file find function

sub cleanup {

if ((/$f/))#$f= appneding "." and file extension for eg:
$f=.pl,$f=.txt
{
$path=$File::Find::name; #$path will contain the
complete
path of a file

@p=split(/\//,$path); #just get the file name

$cnt=@p;
if($p[$cnt-1] eq $temp)
{
print "\n File Found",$path;
}



}

}


If i run this script i am getting the error as "Invalid top directory
at
d:\perl\lib\file\find.pm line 562, line164"

Pls help me.


Thanks in advances :)

Re: Invalid top directory at d:/perl/lib/file/find.pm line 562

am 27.09.2007 11:45:32 von usenet

On Sep 27, 1:29 am, ninad...@gmail.com (Perler) wrote:
> [a multipost]

Please don't multipost.


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

Re: Invalid top directory at d:/perl/lib/file/find.pm line 562

am 27.09.2007 13:10:24 von Martijn Lievaart

On Thu, 27 Sep 2007 09:02:29 +0000, Perler wrote:

> Hi, i am using the following script to find a file entered by a user.

Not tested, just some general comments about the code.

use strict;
use warnings;

Don't ever post a script here without strict and warnings enabled. You'll
find a lot of errors yourself if you enable them, no need to ask the
group to solve problems you could have solved yourself.

>
> use Win32::DriveInfo;
> use File::Find;
>
> my @drives = Win32::DriveInfo::DrivesInUse(); #Get the drives on a
> Machine
>
> my $cnt=@drives; #Number of Drives
>
> my $i=$cnt; #Counter assignment
>
> #The following loop appends "/" to the drive letter #if the drive is
> a
> fixed drive.
>
> for($i;$i>0;$i--)
> {
> $type=Win32::DriveInfo::DriveType($drives[$i-1]); if($type==3)#fixed
> drives
> {
> $d[$i-1]=$drives[$i-1].":"."\\","\\";
> }
> }

Ouch. What is that comma there? ^^^^.

Also, array @d will have "holes" as you don't assign the entries that are
not corresponding to a fixed drive.

The concatenation is also not elegant, ":"."\\" is better written as ":\
\".

I would try something like:

my @d;
for (@drives) {
push @d, "$_:\\"
if Win32::DriveInfo::DriveType($_) == 3;
}

>
> $temp=$ARGV[0]; #store filename to be found

Very bad variable name. Also missing 'my'.

>
> my @t=split(/\./,$temp); #Split the filename to get the extension
>
> my $cnt=@t;
>
> $f=".".$t[$cnt-1]; #Append "." and extension

Very bad variable name.

my $extension = $tmp;
$extension =~ s/.*\././;

>
> find(\&cleanup,@d); #standard file find function
>
> sub cleanup {
>
> if ((/$f/))#$f= appneding "." and file extension for eg:
> $f=.pl,$f=.txt

I don't grok this comment at all. Also,m this will produce false
positives. ITYM

if (/$f$/)

> {
> $path=$File::Find::name; #$path will contain the
> complete
> path of a file
>
> @p=split(/\//,$path); #just get the file name

The filename is also in $_ here, no need to get it this way.

>
> $cnt=@p;
> if($p[$cnt-1] eq $temp)
> {
> print "\n File Found",$path;
> }
>
>
>
> }
>
> }
>
>
> If i run this script i am getting the error as "Invalid top directory
> at
> d:\perl\lib\file\find.pm line 562, line164"
>

First make it run with strict and warnings enabled. Than put in some
print statements to confirm the variables contain what you think they
contain.

Also, this can be done much more simple. No need to check the extension.
It's part of the filename. Completely redundant. Get rid of all the
extension handling stuff. Something along these lines (untested):



use strict;
use warnings;

use Win32::DriveInfo;
use File::Find;

if (@ARGV != 1) {
die "usage:...";
}

my $file2find = $ARGV[0];

my @drives;
for (Win32::DriveInfo::DrivesInUse()) {
push @drives, "$_:\\"
if Win32::DriveInfo::DriveType($_) == 3;
}

find(\&cleanup,@drives); #standard file find function

sub cleanup {
if ($_ eq $file2find) {
my $path=$File::Find::name;
print "File Found: $path\n";
}
}


HTH,
M4

Re: Invalid top directory at d:/perl/lib/file/find.pm line 562

am 28.09.2007 11:12:18 von ninad.29

On Sep 27, 2:45 pm, use...@DavidFilmer.com wrote:
> On Sep 27, 1:29 am, ninad...@gmail.com (Perler) wrote:
>
> > [a multipost]
>
> Please don't multipost.
>
> --
> The best way to get a good answer is to ask a good question.
> David Filmer (http://DavidFilmer.com)

I am really sorry for multiposting but i was getting an error of mail
delievery failure many times.
Wont happen again.