Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

When using File::Find I put in a directory name but was hoping to print out a message to tell me if I do not have permissions to search and make changes in that directory. This is on a Windows NT.
I know about the "-r" and "-x" to check for read and execute permissions on a file but how would you get it to check a directory?
I tried the "-r" and "-x" and couldnt get it to print a message if I had no permissions to the directory:
use strict; use File::Find; my $startDir = '\directory\myNTpath'; unless (-d $startDir) { die "Directory '$startDir' is not a Directory.\n"; } if(! -r $startDir) #also tried -x and -w here but it never works { #this never prints even if I have no permissions in directory print "no permissions to start Directory\n"; } find(\&processSub, $startDir); sub processSub { print "Found $File::Find::name\n"; }
I really want to create a message if I dont have permission to "$startDir". Why wont my print statement ("no permissions to start Directory") work??

Replies are listed 'Best First'.
Re: File Find permissions check
by hardburn (Abbot) on Jul 07, 2003 at 14:06 UTC

    Why do you want to execute a directory? On *nix, you need execute permissions on a directory in order to scan its contents. On WinNT (IIRC), the file scan permission on directories is a seperate attribute.

    There might be a module in the Win32:: namespace that could help you (perhaps Win32API::File?), but I don't deal with Win32 programming enough to help you there.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      To "scan" the content of a directory under Unix, you need execute *and* read permission. Execute permission gives you the ability to make it your current working directory, and to access files in that directory. You can't get a listing with just execute permission.

      Abigail

      I really only need to check if I have read or write permissions on the directory not execute permissions.
        Pardon me in advance, fellow monks!
        I know it's ugly and just a terrible hack... but, personally, if I have to get the job done and -r is not reliable, I wouldn't mind that much trying
        if(!opendir(DIR,$startDir)){ # ... }else{ closedir(DIR); }

        Finding, installing, testing a module for doing just that seems a bit of an overkill... just my 0.02euros, as usual.