Search code examples
perlnetwork-programmingdrivefile-find

Reading Files from Network Drives in Perl


I am working on a perl script which does a document inventory for pdfs in all directories starting from some root directory on our network. The script runs fine locally, but I can't get it to read files from the network drive. i have strawberryperl

this is the beginning of my code

use strict;
use Excel::Writer::XLSX;
use Cwd;
use Tk;
use File::Find;

my @analystreports;
my @directories;

I used the Tk Gui to get a directory

my $homeDir  = Tk::MainWindow->new->chooseDirectory;

capture all files and folders in current directory

find(\&grabPDF, $homeDir);

sub grabPDF {

    my $file = $_;
    if ($file =~ /\.pdf/g) {

        push @analystreports, $File::Find::name;
    }
}

my network drive looks like this in net use

Local N: Remote \abc-file-01\shared data

Please excuse my beginner code. My question is whether I'm doing something wrong with a network drive or if I have to ask our administrator for privileges. Thanks a ton, Dan


Solution

  • A drive is a drive. You can either pass any of the following strings:

    N:\
    n:\
    N:/
    n:/
    

    You could even pass the UNC name.

    \\abc-file-01\shared data
    //abc-file-01/shared data
    

    Of course, you might have to use some escaping to build the string.

    "N:\\"
    "N:/"
    "n:\\"
    "n:/"
    "\\\\abc-file-01\\shared data"
    "//abc-file-01/shared data"
    

    But that's probably not relevant since you appear to be getting the string from Tk rather than building it.

    There's a bug in your code.

    if ($file =~ /\.pdf/g) {
    

    should be

    if ($file =~ /\.pdf/) {
    

    and probably

    if ($file =~ /\.pdf\z/) {
    

    The g makes no sense there and can cause problems (although I think your specific code doesn't suffer). Get rid of the g.