For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2007 > Invalid top directory at d:\perl\lib\file\find.pm line 562









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Invalid top directory at d:\perl\lib\file\find.pm line 562
Perler

2007-09-27, 3:59 am

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($drive
s[$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, <DATA> line164"

Pls help me.


Thanks in advances :)

John W. Krahn

2007-09-27, 7:59 am

Perler wrote:
> Hi,


Hello,

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


use warnings;
use strict;

> 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($drive
s[$i-1]);
> if($type==3)#fixed drives
> {
> $d[$i-1]=$drives[$i-1].":"."\\","\\";


Your comment above says 'appends "/" to the drive letter' but it looks like
you are appending ':\'?


> }
> }


Or more simply:

my @d = map 3 == Win32::DriveInfo::DriveType( $_ )
? "$_:\\\\"
: (),
Win32::DriveInfo::DrivesInUse();


> $temp=$ARGV[0]; #store filename to be found
>
> my @t=split(/\./,$temp); #Split the filename to get the extension


perldoc File::Basename


> 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


In the split above you are escaping the period but here you are not so it will
match any character and the pattern is not anchored so it can match anywhere
in the string, not just at the end.
Why test for the extension *and* compare the complete file name?


> {
> $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)


Why not just use the file name provided in the $_ variable (like you are doing
with the file extension test above?)


> {
> print "\n File Found",$path;
> }
> }
> }


Or more simply:

my $temp = $ARGV[ 0 ]; #store filename to be found
find sub {
print "File Found $File::Find::name\n" if $_ eq $temp
}, @d;


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


This script doesn't have 562 lines and nowhere are you using a DATA filehandle
so it is hard to say why the error message is appearing.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Paul Lalli

2007-09-27, 7:01 pm

On Sep 27, 8:37 am, kra...@telus.net (John W. Krahn) wrote:
>
> This script doesn't have 562 lines and nowhere are you using a DATA filehandle
> so it is hard to say why the error message is appearing.


John, the error message is happening File::Find's line 562, not the
OP's script's line 562. Looks like File::Find is die()ing where it
should be croak()ing, personally.

Basically the OP passed a list of top directories that contained one
or more undefs, probably because of the way he built @d using an if
statement inside the loop.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com