Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

deleting windows subdirectories

by Anonymous Monk
on Jun 04, 2002 at 15:40 UTC ( #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
Node Status?
node history
Node Type: perlquestion [id://171519]
Approved by virtualsue
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2018-07-21 23:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (450 votes). Check out past polls.

    Notices?