Click to See Complete Forum and Search --> : Deleting folders that are older then X Days


sharapov
09-19-2003, 06:17 PM
My backups run every day and generate a folder(directory) with *.sql files in it. How can I go through a spesific directory and delete all folders (with contents) contained in this directory that are older then 10 days (for example) from today's date.

Thanks.

pyro
09-19-2003, 06:32 PM
First, you can use filemtime() (http://us3.php.net/manual/en/function.filemtime.php) or getlastmod() (http://us3.php.net/manual/en/function.getlastmod.php) to check the dates of the file, then you can use something like rmdir() (http://us3.php.net/manual/en/function.rmdir.php) to remove the files (if the directory is empty) or you can use a Unix shell command, if they are not, like this:

`rm -r /yourdir/subdir`;

Note that the above is quite dangerous, as it will remove the directory specified and all subdirectories. Be sure you don't make any mistakes if you use that one...

sharapov
09-19-2003, 06:39 PM
Will that work in the script? I thouight "rm" is the command line only tool?

pyro
09-19-2003, 06:44 PM
It uses the backtick operator (I probably should have specified that) to run shell scripts: http://us3.php.net/manual/en/language.operators.execution.php

You could also use shell_exec() (http://us3.php.net/manual/en/function.shell-exec.php) or just exec() (http://us3.php.net/manual/en/function.exec.php) and possibily system() (http://us3.php.net/manual/en/function.system.php) to run it.