in reply to File::Path::rmtree on UNC path (updated)

#!/usr/bin/perl -- use strict; use warnings; my $dir = "__poop"; rmdir $dir or warn $!; mkdir $dir or die $!; chdir $dir or die $!; $dir = "../$dir" ; rmdir $dir or die sprintf "Cannot rmdir($dir): %d(%s) %d(%s)", int($!),$!,int($^E),$ +^E; __END__ No such file or directory at test.pl line 6. Cannot rmdir(../__poop): 13(Permission denied) 32(The process cannot a +ccess the file because it is being used by another process) at test.p +l line 10.

Replies are listed 'Best First'.
Re^2: File::Path::rmtree on UNC path (updated)
by rovf (Priest) on Mar 04, 2009 at 13:25 UTC

    You are right. I can't chdir in a directory and then delete it. Which leaves me with the original problem: If I do NOT chdir into it, I get consistently the error message ... changed before chdir, expected ..., and nothing gets deleted. If I chdir into it, its content will be deleted, but the directory itself won't.

    Of course I can do the following strategy:

    1. chdir $sharedir;
    2. rmtree $sharedir,1,1;
    3. chdir 'c:/';
    4. rmdir $sharedir;
    This would indeed remove the directory completely, but I get the nasty error message from rmtree in step (2), so this is not a solution I really like.

    Maybe I have to really write my own "rmtree", or - as this is going to run on Windows only - I shell out and do a system('RMDIR'...).

    -- 
    Ronald Fischer <ynnor@mm.st>
      what if you chdir to the directory above that?
      my ($dir, $subdir) = $sharedir =~ /^(.*)\/(.*)$/; chdir $dir; rmtree $subdir;
        my ($dir, $subdir) = $sharedir =~ /^(.*)\/(.*)$/; chdir $dir;

        Actually, I would write this as

        $chdir "$subdir\\..";
        (should be no peril in this context, isn't it?), but, yes, you are right, this seems to work well!! Great idea!

        -- 
        Ronald Fischer <ynnor@mm.st>