tags:

views:

36

answers:

3

I have a folder, that contains other folders, which may or may not contain other folders, where the files might reside. I want to scan the entire dir structure, and find files larger than 100MB, and move these files to the top level directory.

Whats a good way to accomplish this, in php?

For example:

/data/1/subfolder/id33/big_file.avi
/data/4/another_big_file.avi
/data/56/something/big_file2.avi

I need to move these 3 files, into /data

+1  A: 
class MyFilterIterator extends FilterIterator {
    public function accept() {
        return $this->getSize() > 100*1024*1024;
    }
}

$it = new MyFilterIterator(new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator("/data")));

foreach($it as $file) {
    //move ...
}
Artefacto
In place of `filesize(parent::current())`, how about using `$this->getSize()` or with less *magic*, `$this->current()->getSize()` or, finally, if you really must `parent::current()->getSize()`?
salathe
As soon as I try to exclude the smaller files, it stops reading the lower directories 9even though they have sufficiently large files)
Yegor
Artefacto
@Artefacto, "sick" in the good way, I hope. :)
salathe
A: 

If you use a DirectoryIterator from SPL to browse a directory and even recurse into it if you need. To take the next step, you can use a Filter to filter the results by filesize and then do what you need.

Here's a rough example: http://www.phpro.org/tutorials/Introduction-to-SPL-DirectoryIterator.html#17

CaseySoftware
+1  A: 

Simply scan all subfolders with

$it = new RecursiveDirectoryIterator("/data");
foreach(new RecursiveIteratorIterator($it) as $file) {
    //work with file (which is simplya a path here actually) here
}

you'll add some condition there, such as

if (filesize($file) > 100e6) {
    rename($file, '/data/filename...');
}
Mikulas Dite