Solved

delete folder/contents based on date

Posted on 2013-05-21
13
300 Views
Last Modified: 2013-05-21
I have a test environment where I create a group of sub folders within a parent folder.
Each of the sub-folders has a number of documents in each.

ie.
Parent Folder
    Subfolder A
          file1
          file2
    Subfolder B
          file1

I want to create a cron task that will run a script which will remove any subdirectory that is 2 days old.

My challenge is to cycle thru all contents of parentfolder, examine the date of each folder and if the date is older than 2 day delete the contents that folder and then the folder itself.

I don't know the names of the folders in advance

If I use code such as
readdir($parentFolder)
who do I tell if the item is a folder or a file

This script doesn't need to be recursive, as there are not folders within subFolders
0
Comment
Question by:joomla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 9

Expert Comment

by:TvMpt
ID: 39183663
You can use a simple php page to do that.

Use the stat() to get date info

Exemple_:
$stat = stat('\path\to\directory');
echo 'Modification time: ' . $stat['mtime']; // will show unix time stamp.

Open in new window



Modifiy the script to compare the mtime with current time minus 2 days...

/**
* Delete a file/recursively delete a directory
*
* NOTE: Be very careful with the path you pass to this!
*
* @param string $path The path to the file/directory to delete
* @return void
*/
function delete_recursive($path)
{
    if (is_dir($path))
    {
        $iterator = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );
 
        foreach ($iterator as $file)
        {
            if ($file->isDir())
            {
                rmdir($file->getPathname());
            }
            else
            {
                unlink($file->getPathname());
            }
        }
 
        rmdir($path);
    }
    else
    {
        unlink($path);
    }
}

Open in new window

0
 

Author Comment

by:joomla
ID: 39183727
Hi,
thanks for your follow up and help
I'm not familiar with the RecursiveIteratorIterator class

With slight modification for variable names I get the following error
Fatal error: Undefined class constant 'SKIP_DOTS'

Do I need to declare anything before using the class ?
regards
M
0
 
LVL 9

Expert Comment

by:TvMpt
ID: 39183737
Try to include php_fileinfo.dll as an extension in php.ini
0
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 

Author Comment

by:joomla
ID: 39183749
forgive me for not knowing but is this a windows solution ?
0
 
LVL 9

Expert Comment

by:TvMpt
ID: 39183751
Its not about SO. But did you have PHP version 5.3.x ??
0
 
LVL 9

Expert Comment

by:TvMpt
ID: 39183769
This is supported starting with PHP 5.3.
0
 

Author Comment

by:joomla
ID: 39183843
Sorry
Using 5.2.11
0
 
LVL 9

Expert Comment

by:TvMpt
ID: 39183849
You can try to use only the
new RecursiveIteratorIterator(new RecursiveDirectoryIterator($startpath), RecursiveIteratorIterator::CHILD_FIRST)
0
 

Author Comment

by:joomla
ID: 39183926
thanks for the advice
that works to a large degree

however, it does two things that I hadn't wanted
1. it deletes files in the parentFolder.... I only want to delete subFolders
2. it deletes the parentFolder as well

So, if I call the function    
delete_recursive($parentFolder);
everthing, including the parentFolder is deleted

thanks
m
0
 
LVL 9

Expert Comment

by:TvMpt
ID: 39183930
Just put a simple if to skip base path...
0
 

Author Comment

by:joomla
ID: 39183959
I'm sorry, but how do it apply an if statement to check for the base path
I'm not familiar with this class
0
 
LVL 9

Accepted Solution

by:
TvMpt earned 500 total points
ID: 39183976
Try to ignore the self remove path

function delete_recursive($path)
{
    if (is_dir($path))
    {
        $iterator = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );
 
        foreach ($iterator as $file)
        {
            if ($file->isDir())
            {
                rmdir($file->getPathname());
            }
            else
            {
                unlink($file->getPathname());
            }
        }

    }
}

Open in new window

0
 

Author Closing Comment

by:joomla
ID: 39184012
thanks for help
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question