Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

deleting windows subdirectories

by Anonymous Monk
on Jun 04, 2002 at 15:40 UTC ( [id://171519]=perlquestion: print w/replies, xml ) Need Help??

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

Hello As you can see my code is simply a system call that uses windows "DEL" command with a few parameters. It successfully deletes all files including subdirectory files, but will NOT delete the subdirectories themselves. Any suugesstions?
use strict; print "\nRemoving $workspace directory\n\n"; system "DEL \/F \/Q /S $workspace";

Replies are listed 'Best First'.
Re: deleting windows subdirectories
by stajich (Chaplain) on Jun 04, 2002 at 15:50 UTC
    Try using File::Path
    use File::Path; rmtree(['/foo/bar/baz', $directory2],0,1);
    Putting a 1 as the second param will have rmtree print a msg about whether or not it is rmdir or unlinking a dir/file.
Re: deleting windows subdirectories
by tune (Curate) on Jun 04, 2002 at 15:42 UTC
    Try DELTREE. (Though not Perl-related)

    --
    tune

Re: deleting windows subdirectories
by Steve_p (Priest) on Jun 04, 2002 at 17:03 UTC
    Try
    use strict; use File::Find; my $workspace = "c:/foo/bar"; find \&delFiles, $workspace; sub delFiles { if(-f) { # unlink $File::Find::name or die "Unable to delete file: $!"; print "$File::Find::name"; } }
    The print is there for testing purposes. To make it work, you'll just need to uncomment the unlink.
    Update: I forgot to die and added it based on grinder's suggestion.

      That's not going to work and you're not going to have any idea why, because you're not attempting to check whether the system call (unlink) fails, and if so, for what reason ($!).

      The code will try to delete a directory first, before attempting to delete the files within said directory. At the very least, it should do a depth first search. Even so, be careful about the . and .. entries.

      Use the rmtree approach instead.


      print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
        The unlink on the directory will only be tried if it read
        unless(-f){ unlink $File::Find::name; }
        The if(-f) prevents you from deleting anything but files. That means the directories are left alone. True, I should have a die, but it does work, and I use similar code regularly.
Re: deleting windows subdirectories
by Rex(Wrecks) (Curate) on Jun 04, 2002 at 16:54 UTC
    Update: BTW, the File::Find solutions listed here will be much better for implimentation. They won't impose any of the restrictions that system() invariably does.

    deltree will work for you on Win9x systems.

    For NT4/Win2K/WinXP I suggest rd /s/q <dirname> This works much better.

    This is OT but you might want to check out the O'Reilly books on Windows commands (they have several of the small pocket reference ones dedicated to this). You will find the NT4/Win2K/WinXP commandline tools to be quite handy. Not quite as robust as a typical Unix shell, but getting better with every release.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found