Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

delete old files (windoze)

by softworkz (Monk)
on Oct 15, 2004 at 13:45 UTC ( [id://399501] : perlquestion . print w/replies, xml ) Need Help??

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

Hi everyone!

I have this script that I wrote and I need some comments on it. I want to clean (delete) old files in the temp directory and it seems to work on my local machine but when I run it on a test server (as administrator) I'll get errors on files were not found (I believe it's File::Find) but if I do a windoze search I can't find the files either (folder options set to view EVERYTHING) I'm also wondering which way would be the CORRECT way to actually delete the files on windoze and how can I get around the files not found?
#!/usr/bin/perl -w use strict; use File::Find; use File::stat; use Time::localtime; my $NodeName = Win32::NodeName(); my $path = ("C:\\TEMP\\"); my $LogDir = ("C:\\LOGS\\"); # log folder #date and time &TimeStamp; sub TimeStamp { my $tm = localtime; my $Date = sprintf ("%02d_%02d_%04d", $tm->mon+1, $tm->mday, $tm-> +year+1900); my $Log = $Date."_".$NodeName.".txt"; open(LOG,">>$LogDir\\$Log") || die " can't open $Log fo +r writing: $!"; find (\&unwanted, $path); } sub unwanted { # Change the age to desired days past to delete my $age = 1; my $atime = int(-A _); my $mod_time = int(-M _); if (!(/^\./) && $mod_time > $age && $atime > $age) { print LOG "going to delete these! $_ modified days old -->$mo +d_time\n"; #system ("del $_ /Q"); #unlink($_); }else{ print LOG "KEEPING $_ access days old ---> $atime\n"; } } # END

Replies are listed 'Best First'.
Re: delete old files (windoze)
by xorl (Deacon) on Oct 15, 2004 at 13:55 UTC
    This is how I do it.
    use File::Find; find(\&wanted, $dir); sub wanted { unlink $_ if -M $_ > $days; }
    $dir is the directory you want to delete files from
    $days is how old the files are. i.e. if you wanted to delete all files older than two weeks set days to 14.
Re: delete old files (windoze)
by kutsu (Priest) on Oct 15, 2004 at 14:01 UTC

    Do the files exist and is your test server running windows? On command line type locate filename for linux or dir /s /p filename on windows (may have to do a cd \ before hand).

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      I tried dir /s /p within the directory (not found) I'm beginning to suspect SPACES in file names..

        If you were in the TEMP directory and did a dir /s /p and found nothing...TEMP is empty, it has nothing to do with spaces in the filename. I would guess this works on your local machine because you don't clean out the TEMP directory, on a server (even a test server) this proably isn't the case and your files are getting deleted, which is one of the many reasons you shouldn't put non-temporary files in a temp directory. However, if you don't even understand the basic function dir (like how to find files with spaces in their names), I suggest you take some time and learn about your OS.

        "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce