Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Perl Script Just Won't Work

Options
  • 07-07-2005 6:23pm
    #1
    Registered Users Posts: 902 ✭✭✭


    Hopefully one of you guys will be able to spot something in this script that I must be missing. I'm at the stage of throwing the monitor out the window...

    What it is supposed to do is take a folder and a specific number of days. It checks each subfolder in the specified folder for its last modification date. If it is older than the specified days then it uses rmtree to remove the subfolder (this is running on windows). It also does some other things like write to a log file but this part is working.

    The problem is it deletes every folder in the specified folder sometimes and then just the older ones (as it is supposed to do) other times. The way I'm replicating the folder structure is by creating a folder, turning back the clock on my PC by a month, creating a folder, etc, and at the end put the date back to the correct date. Hence simulating having older folders.

    Here's the code, thanks in advance for the help:



    use File::Find;
    use File::Path;

    $Days = 45;
    $Dir_Path = "D:/test";
    $LogFileLocation = "D:/log.txt";

    open(OUTPUTFILE, ">>$LogFileLocation")|| die "open0: $!";
    $Del_Date = localtime (time);

    print OUTPUTFILE "
    Open log $Del_Date
    \n";
    print OUTPUTFILE "Folders deleted in $Dir_Path over $Days days old:\n";

    find(
    sub
    {
    $_ =~ tr/\//\\/;

    if ( -d $_ && -M $_ > $Days )
    {
    print "$_ is older than $Days days\n";
    print "Deleting $_ now\n";
    print OUTPUTFILE "$_\n";
    rmtree ("$_",1,1) || print "Unable to delete $_";
    $deletions++;
    }
    else
    {
    print "$_ is younger than $Days days\n";
    }

    }, $Dir_Path);

    if ($deletions)
    {
    print "$deletions folders(s) over $Days days were deleted.\n";
    print OUTPUTFILE "$deletions folders(s) over $Days days were deleted.\n";
    }
    else
    {
    print "No folders over $Days days old detected.\n";
    print OUTPUTFILE "No folders over $Days days old detected.\n";
    }

    print OUTPUTFILE "
    Close log
    \n\n";
    close(OUTPUTFILE) || die "close0: $!";


Comments

  • Registered Users Posts: 1,268 ✭✭✭hostyle


     print "$_ is older than $Days days\n";
    

    Does this always correctly tell you all directories that its deleting? If so it sounds like the windows filesystem -M time isn't to be trusted.


  • Registered Users Posts: 902 ✭✭✭thesteve


    The output from this statement the last hundred times when it has gone wrong is:


    Open log Sat Jun 25 11:58:31 2005
    Folders deleted in D:/test over 45 days old:
    .
    1 folders(s) over 45 days were deleted.
    Close log


    It seems to be finding that the directory passed (D:/test/) is over the 45 days and then deletes all its subdirectories. What its supposed to be doing is checking each subdirectory and not the passed directory itself (although this shouldnt matter as this directory is modified everyday with the creation of a new subdirectory so should never meet the requirements for deletion!).

    You can see my confusion... :(


  • Registered Users Posts: 902 ✭✭✭thesteve


    Apologies, thats the output from the log file, the output on screen is:

    . is older than 45 days


  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    It's too late to read your script properly .. but do you want to delete d:\test .. or just the old dirs inside d:\test. If it's the latter then you're picking up on "." which is the current directory (d:\test).. so you need to exclude that in your if ...
    if( -d $_ && $_ ne "." && -M $_ > $Days ) {...
    


Advertisement