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

Discussion in 'Perl Misc' started by Perler, Sep 27, 2007.

  1. Perler

    Perler Guest

    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 :)
    Perler, Sep 27, 2007
    #1
    1. Advertising

  2. Perler

    Guest

    On Sep 27, 1:29 am, (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)
    , Sep 27, 2007
    #2
    1. Advertising

  3. 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, <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
    Martijn Lievaart, Sep 27, 2007
    #3
  4. Perler

    Perler Guest

    On Sep 27, 2:45 pm, wrote:
    > On Sep 27, 1:29 am, (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.
    Perler, Sep 28, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. scad
    Replies:
    23
    Views:
    1,161
    Alf P. Steinbach
    May 17, 2009
  2. Kenjis Kaan
    Replies:
    4
    Views:
    123
    Randy Kobes
    Jun 29, 2003
  3. SRB

    invalid top directory

    SRB, Dec 25, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    307
  4. Replies:
    0
    Views:
    223
  5. Oliver Soeder
    Replies:
    1
    Views:
    241
Loading...

Share This Page