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

I have the following code to remove a directory recursively:

# Looking for entries in \\myhost.mycompany.com\results$\ matching + a certain glob pattern my @sharedirs=glob("//myhost.mycompany.com/results\$/ART/$use +rname*$uidstr"); if(@sharedirs==0) { print "No result share found\n"; } elsif(@sharedirs > 1) { print("No unique result share found\n", } else { my $sharedir=$sharedirs[0]; print "Removing $sharedir\n"; print "Removing of $sharedir was not successful\n" if rmtree +($sharedir,1,1)==0; }
Running this code for some specific values always yields with Perl 5.8.8 something like:
Removing //myhost.mycompany.com/results$/ART/fischron_st_st_buildlsf0. +xml_UID1235122965 Directory //myhost.mycompany.com/results$/ART/fischron_st_st_buildlsf0 +.xml_UID1235122965 changed before chdir, aborting at ...
Perl 5.10 gives a little bit more information:
Removing //myhost.mycompany.com/results$/ART/fischron_st_st_buildlsf0. +xml_UID1235122965 Directory //myhost.mycompany.com/results$/ART/fischron_st_st_buildlsf0 +.xml_UID1235122965 changed before chdir, expected dev=2 inode=0, actu +al dev=-1 ino=0, aborting at ...
I don't know what the device numbers mean in a Windows environment, but any idea how I could solve the problem (lest of writing my own rmtree code)?

UPDATE

I found the error go away by doing a chdir first, i.e.
chdir($sharedir); rmtree($sharedir,1,1);
Now it works a lot better, BUT although rmtree can remove all the files within $sharedir, it complains "Permission denied" on $sharedir itself (and also unlink $sharedir can't erase it. Well, you might say, this just means that $sharedir is read-only. But the funny thing is that I can easily remove the directory from the command line:
RMDIR \\myhost.mycompany.com\results$\ART\fischron_st_st_buildlsf0.xml +_UID1235122965
-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: File::Path::rmtree on UNC path (updated)
by Anonymous Monk on Mar 04, 2009 at 12:54 UTC
    #!/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.

      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;