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

I just want to tell perl to delete all the files in a given win32 directory(active state). How hard can that be? How much of a newbie can I be? If I did it I'd have to get a list from the directory, cycle through, and delete each file. Is there a cooler way to do it? Oh yeah. I want to secure delete shred these files too.

Replies are listed 'Best First'.
Re: Deleting Files on Win32
by ikegami (Patriarch) on Apr 24, 2007 at 22:01 UTC

    If you want to delete a directory and all of its the subdirectories, use rmtree from File::Path.

    The alternative is not very hard.

    use File::Spec::Functions qw( catfile ); my $dir = ...; opendir(my $dh, $dir_name) or die("Unable to list contents of dir \"$dir_name\": $!\n"); while (defined(my $file_name = readdir($dh))) { my $full_name = catpath($dir_name, $file_name); next unless -f $full_name; unlink($full_name) or warn("Unable to delete file \"$full_name\": $!\n"); }

    I want to secure delete shred these files too.

    rmtree and unlink don't do that. It might be easier and more reliable to use an existing tool (directly or by calling it from Perl if you want some automation).

Re: Deleting Files on Win32
by Old_Gray_Bear (Bishop) on Apr 25, 2007 at 00:28 UTC
    There are a variety of tools available to "secure delete shred" files, see Free Country's web site.

    Note: There is some questions as to whether it is truly possible to 'shred' a magnetically recorded file. I have had hall-way chats at conferences about 'secure deletion of magnetic media' with folks who have a strong interest in the topic. The consensus seems to be that short of physically destroying the media in a high-temperature furnace, given enough determination (and money) any erasure method can be reversed and a sizable portion of the data retrieved. The operant word in the last sentence is 'money'.

    ----
    I Go Back to Sleep, Now.

    OGB

      Quite a while ago, when I worked for a US helicopter manufacturer, we had to write a security plan for ensuring that classified material on magnetic media could not be read. Our technique was to remove the platter from the hard drive and sand blast off the magnetic medium.

      Overkill is very much in the US military tradition; using a good tape degausser would probably be more than adequate.

      emc

      Insisting on perfect safety is for people who don't have the balls to live in the real world.

      —Mary Shafer, NASA Dryden Flight Research Center

        using a good tape degausser would probably be more than adequate

        Ahh .. the good old tape degausser. I've ruined a number of watches (and probably a few brain cells) while running 9 track reels through em. Hmmm ... I need to find that release form that said I wouldn't hold the company liable if I became ill from using the degausser (nothing like forced contracts).

        -derby
Re: Deleting Files on Win32
by jdporter (Paladin) on Apr 25, 2007 at 22:10 UTC
    I want to secure delete shred these files

    You might check out Shred, Wiper, Cleaning up your hard disk, and Bitwise File Shredding.
    There's some good stuff in those threads, in addition to the codes presented in the root nodes.

    But I'm also offering my own solution: overwrite a file.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: Deleting Files on Win32
by naikonta (Curate) on Apr 25, 2007 at 06:41 UTC
    Please Choose a Good, Descriptive Title next time you post question.

    (Ugh, I couldn't link right to #title)


    Update: fixed links to #title (thanks, ysth :-)

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!