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

Hello Wise Monks!
I am wondering how to best delete all the temporary files on win32 machines for all users.
I thought of something like this:

#Pseudo Code #!/usr/bin/perl use warnings; use strict; use File::Find; my $startdir = "C:/Dokumente\ und\ Einstellungen/"; my $subdir1 ="Lokale\ Einstellungen/ temp"; my $subdir2 ="Lokale\ Einstellungen/Temporary\ InternetFiles"; opendir(DIR, $startdir) or die "could not open: $!"; while (my $file = readdir(DIR)) { next if ($file =~ /^\.{1,2}|All\ Users|LocalService|NetworkService +|Default\ User/i); print "$file\n"; print "$subdir1\n"; print "$subdir2\n"; #missing part: delete code from $file, $subdir1 recursively } closedir(DIR);
#Missing Part:
#Code to recursively go down in the directories and
#delete from the starting point e.g. "C:/Dokumente und Einstellungen/user1/Lokale Einstellungen/Temp" #the same for user2 ... userx

Thanks
MH
  • Comment on Win32: Deleting Temporary directories of all users on a machine (recursive)?
  • Download Code

Replies are listed 'Best First'.
Re: Win32: Deleting Temporary directories of all users on a machine?
by BrowserUk (Patriarch) on Jan 13, 2010 at 07:57 UTC

    How about

    my $dir = "C:/Dokumente und Einstellungen/user1/Lokale Einstellungen/T +emp"; $dir =~ s[/][\\]g; system qq[rd /s /q "$dir"];

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

        The built-in rmdir function will only remove a directory if it is empty.

        del /s descends directories deleting the files, but leaves empty subdirectories in place.

        rd /s gets shot of everything (assuming permission).


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Win32: Deleting Temporary directories of all users on a machine (recursive)?
by cdarke (Prior) on Jan 13, 2010 at 09:08 UTC
    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: $!"; } }
      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
      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$/;