Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

rmtree deletes everything inside the folder but doesn't deletes the root folder him self

by Nelly (Novice)
on Apr 28, 2004 at 08:42 UTC ( #348757=perlquestion: print w/ replies, xml ) Need Help??
Nelly has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I'm trying to move folder that include another folders and files. My actions are:
my $file2Copy = File::NCopy->new(recursive => 1); $file2Copy->copy($folder,$ArcFolder); rmtree($folder, 0, 0);
Every thing is good but it doesn't deletes the root folder that is already empty.
The error message I'm getting is:
Can't remove directory C:\125\Ba_hu\\update35: Permission denied
What is happening? rmtree has deleted everything inside this folder,
but can't delete the root folder that is empty.
I've tried to do:
rmdir $folder;
after rmtree and it's not deleting the folder any way, Please help!!!

Comment on rmtree deletes everything inside the folder but doesn't deletes the root folder him self
Select or Download Code
Re: rmtree deletes everything inside the folder but doesn't deletes the root folder him self
by mce (Curate) on Apr 28, 2004 at 08:57 UTC
    Hi,

    Just a small remark, but the script is't executed from within the directory you are trying to move, is it?

    The permission denied means -most likely- that there is a lock on this directory. So some process is running from within this directory.


    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    BMC, Belgium
      Ofcourse, the script isn't running from the directory I'm trying to move.
      But, How can I unlock this directory? chmod didn't helped me.
        Check the permissions on the directory the one you wish to move is in. Also, if you are on an OS that uses it, check the extended error $^E

        This sounds like the classic "in-use" problem. Even if the process doing the deletion doesn't have the directory to be deleted as one of it current directories-- every process has a current directory for each logical drive visible to it!--if any other process in the system is using that directory, directly or indirectly, then permission to delete that directory will be denied.

        As a simple example: Say you have a system with 2 drive C: & D:, the directory your trying to delete is d:\foo. You have a shell window running, and at some point you switch to the d: drive and cd to \foo....then you switch back to the c: to run the script to delete d:\foo. You get permission denied.

        Even though you are running the script at the C:\perl prompt, that shell session still has a handle to d:\foo, and so the system won't let you delete it. Right or wrong, that is the way the system works, and you have to work within that constraint.

        The one that usually catches me out is when I have edited a file in the target directory tree and even though I have closed the file, the file dialog retains the last directory I visited as the default location for the next time I want to open a file. So, even though I have no file open in that directory, the file dialog retains a handle to it, and I am prevented from deleting it until I either close the editor, or bring up the file dialog and switch to a different directory.

        The really horrible bit is that there are many file dialogs in the system, and each retains it's own sense of the CWD. It can be a real pain working out what and where the open handle is being retained.

        I'm sorry, but I don't have a solution to this, just a logical (if neither official nor what you want to hear) explanantion.

        The explorer seems to have some special logic built in so that if you right-click delete a subtree, the root directory of that subtree, which you made a CWD when you highlighted it, doesn't disappear until you move to another location, and it then is deleted. Something similar to the unix "mark for deletion" process. Unfortunately, this seems to a be a property of the explorer, rather than the underlying OS, and worse, even the explorer doesn't seem to be able to get it right all the time.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
      You need to select the "no_chdir" option so it doesn't change to the directory you're acting on: find({ wanted => \&process, no_chdir => 1 }, '.');
Permission denied to delete an empty folder
by Nelly (Novice) on Apr 28, 2004 at 10:42 UTC
    I'm trying to delete an empty folder with rmdir function, but I'm getting an error message :
    Permission denied.....
    How can I solve this problem? Thanks ahead!!!!!

      Check the permissions of the folder, correct them, then rerun your script :-)

      Seriously: which ownership runs your script under? which ownership/permissions are set on the folder?

      Ciao!
      --bronto


      The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
      --John M. Dlugosz
        Thanks for replying, but how can I do it, "Check the permissions of the folder, correct them"
        I'm new in perl,excuse me if it's very obvious!

      Additionally to what bronto said, under Win32, an application that is still running and has the directory in question as its current working directory will also prevent the directory from being deleted.

      Note that the permissions for deleting the folder aren't the permissions on the folder, they are the write permissions of the parent!. I got bitten by that, once or twice - usualy when I didn't want to delete that specific folder, but wanted to delete all the other files and folders.
      How can I Check the permissions of the folder,and correct them? I'm new in perl,excuse me if it's very obvious!
        check:
        ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);
        set:
        chmod
        see perlfunc for more details stat chmod.

        pelagic
        What operating system? It's rather hard to guess the exact commands unless you at least give us that...

        What have you tried so far? In what way has it failed?

        You would realy benifit from reading How to ask questions the smart way

        Well I don't know your system but on Win32 its tricky. Here is how. Its actually a subroutine to check if a directory is empty and then remove it (of course you could just force it to remove it with the the "rmtree" command, but better to be safe).

        Also uncomment the commented-out section if you want it to display the folder permissions mask before you alter them.

        What I don't like about this code is that doing a directory listing to check if a directory is empty is slow, especially if a directory is large. There could be an easy command to do that that I'm simply not aware of (probable!).
        #!perl use Win32::FileSecurity; use Cwd qw(cwd); use File::Path; use strict; use warnings; sub remove_dir { my ($path) = @_; my $currpath = cwd; my $ok = chdir $path; if ($ok) { my @dir = <*>; ## check if directory is empty if (!@dir) { my $dirmask = Win32::FileSecurity::MakeMask( qw(FULL GENER +IC_ALL) ); my %hash; # if ( Win32::FileSecurity::Get( $path, \%hash ) ) { # while( (my $name, my $mask) = each %hash ) { # print "$name:\n\t"; # my @happy; # Win32::FileSecurity::EnumerateRights( $mask, \@happy +) ; # print join( "\n\t", @happy ), "\n"; # } # } Win32::FileSecurity::Get( $path, \%hash); $hash{Administrator} = $dirmask; $ok = chdir $currpath; if ( Win32::FileSecurity::Set($path, \%hash) ) { print "\n\t$path --> Permissions Changed"; } else { print "\n\t$path --> Permission change failed$!\n$^E"; } if (rmtree $path) { print "\n\t$path --> Removed"; } else { print "\n\t$path --> Can't be removed $!\n$^E"; } } } }

        Dean
        The Funkster of Mirth
        Programming these days takes more than a lone avenger with a compiler. - sam
        RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers
Re: rmtree deletes everything inside the folder but doesn't deletes the root folder him self
by Anonymous Monk on Apr 29, 2004 at 09:28 UTC
    You could maybe replace your rmtree() command by:

    'rm -rf $folder'

    Just my 2 cents...
      Thanks for replying, I've tried to follow your suggestion,and I've recieved error:
      Useless use of subtraction (-) in void context at C:\Perl\ArcUpdFdr.pl line 64.
      Bareword "rm" not allowed while "strict subs" in use at C:\Perl\ArcUpdFdr.pl line 64.
      Can you tell me, how can I use this function, I couldn't find the help on it. Tnanks!
        that's not a bare word. put it in backticks! `rm -rf /dir/`;
Re: rmtree deletes everything inside the folder but doesn't deletes the root folder him self
by ikegami (Pope) on Jan 06, 2010 at 18:50 UTC
    The most common cause of inability to delete a directory due to permission issues is that some application has it as its work directory.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://348757]
Approved by mce
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-07-24 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (157 votes), past polls