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

P

Perler

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, <DATA> line164"

Pls help me.


Thanks in advances :)
 
M

Martijn Lievaart

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, <DATA> 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top