in reply to Perl Permissions

Hi. You are getting permission denied, because you are attempting to open a directory as a file

Your readdir is pulling in all the entries in your /perl/ass folder including subdirectories. You then attempt to open one of those subdirectories as a file.

You might want to use the -d operator to test if $currentlog is a directory or not before attempting to open it.

Replies are listed 'Best First'.
Re^2: Perl Permissions
by bart (Canon) on Dec 04, 2006 at 12:36 UTC
    I agree with bingos. Don't get thrown off by the error message by Windows, you'll indeed get a "Permission denied" error message if you try to pull one of the stunts that bingos said: trying to open a file as a directory, or vice versa. But it could have some other, but similarly esotheric cause. Just don't stare yourself blind on the word "permission".
      ah yeah makes sense now thanks :D , my mate said to say your the shiz!! lol
      I looked up your -d and also found the -T which i am useing as i am only dealing with text files,
      so i do this now to avoid directory's:
      if ( -T $currentlog ) #checks wether the file is a text f +ile { open(CURRENTLOG, $currentlog)|| die "file not found: $!"; } else . . .
      However i am now getting the error
      "Permission denied at tempCHRONO.pl line 76, <CURRENTLOG> line 1"
      on the code
      $oldlocation = "/perl/ass/$logfiles[$hour]"; #Cut and Pastes + the log file into corresponding folder $newlocation = "/perl/ass/$folder[$N]/$logfiles[$hour]"; print " old = $oldlocation \n new = $newlocation\n"; move($oldlocation, $newlocation) or die "File cannot be mo +ved: $!";
      I didnt really wanna post all my code as that is not me solving the error myself at all but if it helps :
      #!/usr/bin/perl use Date::EzDate; #Requests to use the +date mod use File::Copy; my $currentdate = Date::EzDate->new(); #Loads the current Da +te an Time # #Manageing Log Files Chronologically # my $hour = 2; until ($hour > 26) #Loo +p to read all log files { my $dateloaded = Date::EzDate->new(); #tak +es todays date opendir(LOGS, "/perl/ass/")|| die "dir not found"; #Ope +ns the folder containing the days log files my @logfiles = readdir(LOGS); #Put +s the name of all files into an array closedir(LOGS); my $currentlog = "/perl/ass/$logfiles[$hour]"; if ( -T $currentlog ) #checks wether the file is a tex +t file { open(CURRENTLOG, $currentlog)|| die "file not found: $!"; } else { $hour++; } $firstline = <CURRENTLOG>; $date = substr($firstline,0,2); #Ta +ke the day of month from the log $date2 = substr($firstline,3 ,2); #Ta +kes the month from the log $today = $currentdate->{'{day of month}'}; #To +days Date $month = $currentdate->{'month number'} +1; #To +days Month $dateloaded -= 7; #mi +nus a week $history = substr($dateloaded,8 ,2); #1- +week-ago $dateloaded -= 7; #fo +r the whole week $historyformer = substr($dateloaded,8 ,2); #En +d 1-week-ago $N = 0; until ( $N == 3) #Un +til all 3 weeks have been checked { @folder = qw( 1-week-ago 2-weeks-ago 3-week-ago ); if ( $date >= $historyformer && $date <= $history ) #if + the log equals greater than a week ago but less than 2 weeks ago { $month--; if ( $date2 == $month || $date2 == $month ) { $oldlocation = "/perl/ass/$logfiles[$hour]"; #Cu +t and Pastes the log file into corresponding folder $newlocation = "/perl/ass/$folder[$N]/$logfiles[$hour]"; print " old = $oldlocation \n new = $newlocation\n"; move($oldlocation, $newlocation) or die "File cannot be mo +ved: $!"; #DOESNT WORK $N = 3; $date = 999; #Ma +kes the date not equal to any other date $month++; } } $N++; $history = substr($dateloaded,8 ,2); $dateloaded -= 7; $historyformer = substr($dateloaded,8 ,2); #En +d 1-week-ago } $date = substr($firstline,3 ,2); #Co +llect the month from the log $N = 0; until ( $N == 6) #Un +til all 6 months have been checked { @folder = qw( 1-month-ago 2-months-ago 3-months-ago 4-months +-ago 5-months-ago 6-months-ago); $month--; #C +ounts down 6 months if ( $date == $month ) #i +f the first line of the log equals the date { $oldlocation = "/perl/ass/$logfiles[$hour]"; #C +ut and Pastes the log file into corresponding folder $newlocation = "/perl/ass/$folder[$N]/$logfiles[$hour]"; print " OLD - $oldlocation \n NEW - $newlocation"; move($oldlocation, $newlocation) or die "File cannot be mo +ved: $!"; #DOESNT WORK $N = 3; $date = 999; #M +akes the date not equal to any other date } $N++; } #if ($date == $month -1) # { # DELETE; # } $hour++; } exit;
      And no laughing because it isnt exactly finished :P
        You might get a "permission denied" error if the directory for the target doesn't exist. In other worlds, if in
        $newlocation = "/perl/ass/$folder[$N]/$logfiles[$hour]";
        the directory
        $newlocationdir = "/perl/ass/$folder[$N]";
        doesn't exist.

        You can use mkpath in File::Path (a standard module, so you should already have it on your system) to create it, just in case, before moving the file:

        use File::Path 'mkpath'; use File::Copy 'move'; mkpath "/perl/ass/$folder[$N]"; move($oldlocation, $newlocation) or die "File cannot be moved: $!";
        Note that there is no error checking for mkpath, because it may fail if the directory already exists. It doesn't matter. But the directory should exist after mkpath, so a test with -d "/perl/ass/$folder[$N]" would not be a bad idea.