Solved

# delete folder/contents based on date

Posted on 2013-05-21
291 Views
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 Question by:joomla • 7 • 6 13 Comments LVL 9 Expert Comment 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.  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); } }  0 Author Comment 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 Try to include php_fileinfo.dll as an extension in php.ini 0 Author Comment forgive me for not knowing but is this a windows solution ? 0 LVL 9 Expert Comment Its not about SO. But did you have PHP version 5.3.x ?? 0 LVL 9 Expert Comment This is supported starting with PHP 5.3. 0 Author Comment Sorry Using 5.2.11 0 LVL 9 Expert Comment You can try to use only the new RecursiveIteratorIterator(new RecursiveDirectoryIterator($startpath), RecursiveIteratorIterator::CHILD_FIRST)
0

Author Comment

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 Just put a simple if to skip base path... 0 Author Comment 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 TvMpt earned 500 total points 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());
}
}

}
}

0

Author Closing Comment

thanks for help
0

## Featured Post

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…