in reply to Win32: Deleting Temporary directories of all users on a machine (recursive)?

Here is a recursive subroutine I use on Windows and Linux:
zapfile ($subdir); sub zapfile { my ($file) = shift; print "Removing $file...\n"; if (-d $file) { opendir(my $dh, $file) or die "Unable to open $file: $!"; while (my $name = readdir($dh)) { next if $name eq '.' or $name eq '..'; # Recursive call! zapfile ("$file/$name"); } closedir $dh; rmdir ($file) or die "Unable to rmdir $file: $!"; } else { unlink $file or die "Unable to remove $file: $!"; } }
  • Comment on Re: Win32: Deleting Temporary directories of all users on a machine (recursive)?
  • Download Code

Replies are listed 'Best First'.
Re^2: Win32: Deleting Temporary directories of all users on a machine (recursive)?
by matze77 (Friar) on Jan 13, 2010 at 11:07 UTC
    Wow. Thank alot. Dont even have to call modules ...
    Note to myself read about subroutines functions again...
    Subroutines

    Thanks
    MH

      If you wanna module, then... rmtree() in File::Path seems to work pretty well.
      #!/usr/bin/perl -w use strict; use Data::Dumper; use File::Path; my $Dir2Delete = 'C:/temp/JUNK'; rmtree( $Dir2Delete, { verbose => 1, error => \my $err_list } ); print Dumper $err_list; #should be $VAR1 = [];, ie null
Re^2: Win32: Deleting Temporary directories of all users on a machine (recursive)?
by matze77 (Friar) on Jan 13, 2010 at 14:20 UTC
    Ok. I wonder how to take the list from the startdir and go for each element, what would you suggest put the list into an array and push through the array till it is empty?:
    21.08.2009 11:03 <DIR> Administrator 08.01.2010 13:38 <DIR> administrator.Domain 06.07.2009 08:27 <DIR> All Users 16.06.2009 13:08 <DIR> user 18.06.2009 13:08 <DIR> user2
    Namely administrator, administrator.Domain, user and so on. So i would construct a new string?

    $subdir = $startdir . $user . $Tempdir;
    Where $startdir = "Dokumente/ und/ Einstellungen";
    $user = @array[0]
    $tempdir = "Lokale\ Einstellungen\TEMP";
    Construct the path for user1 #e.g. "C:/Dokumente\ und\ Einstellungen/user/Lokale\ Einstellungen/Tem +p" delete recursive construct the path for user2 #e.g. "C:/Dokumente\ und\ Einstellungen/user/Lokale\ Einstellungen/Tem +p" construct the path for lastuser ...
    My Problem is now putting the whole together ...
    Any hints?
    MH
      I am not certain what you are trying to achieve here. Looking at the recursive subroutine I supplied (zapfile), you could place additional name checks after:
      next if $name eq '.' or $name eq '..';
      For example:
      next unless $name =~ /Temp$/;