I got tired of manually organizing my downloads folder, and after not being happy with existing solutions I decided to write a PHP script which would enable me to easily configure new rules for file and folder management. This is achieved by having a flexible class hierarchy, so that you don't end up repeating yourself when configuring new rules and actions.
Currently I have one class for each task, but this could be easily circumvented by using Closures to pass in the desired functionality, instead of writing it inside a custom class method. But the current solution is a bit cleaner, since you can invoke the constructor without knowing about the implementation. An example class can be seen below, which moves all video files greater than 50 Mb which contains "big bang theory" in their file name.
class DirItemCommand_move_files_BigBangTheory extends DirItemCommand_move_files
{
// Given a file name, this returns the target path where the file should be moved
public function getTargetPath($info)
{
return 'C:/Share/series/BBT/' . $info['fname'];
}
public function __construct()
{
parent::__construct(new DirItemFilterCollection(array(
// Ignore folders
'by_type' => new DirItemFilterer_by_type(false),
// Ensure files are greater than 50 Mb
'by_size' => new DirItemFilterer_by_size('50M', '>'),
// _VIDEO_ is a pre-defined set of file extensions
'by_ext' => new DirItemFilterer_by_extension('_VIDEO_'),
// Filenames are matched by regular expressions
'by_name' => new DirItemFilterer_by_filename('big.+bang.+theory')
)));
}
}
An other example moves all folders which contain less than 3 Mb of files into a .Trash folder. Many torrents have cover photos, readmes or sample videos, and these may be left behind when the main files have been moved to somewhere else. It is clear that by leveraging the 250 lines of PHP utility classes, it is trivial to define new automatic tasks.
class DirItemCommand_move_small_folders extends DirItemCommand_move_files
{
public function getTargetPath($info)
{
// Need to use substr because paths start with ./
return '.Trash/' . substr($info['path'], 2);
}
public function __construct()
{
parent::__construct(new DirItemFilterCollection(array(
// Match only folders
'by_type' => new DirItemFilterer_by_type(true),
// Process those which are smaller than 3 Mb
'by_size' => new DirItemFilterer_by_size('3M', '<')
)));
}
}
It is an interesting heuristic that usually movies have their release year listed in their file name. I'm using this to automatically identify and move movie files to the correct storage folder. Thanks to regular expressions, also this task is very easy to implement.
class DirItemCommand_move_files_movies extends DirItemCommand_move_files
{
public function getTargetPath($info)
{
return 'C:/Share/long_movies/' . $info['fname'];
}
public function __construct()
{
parent::__construct(new DirItemFilterCollection(array(
'by_type' => new DirItemFilterer_by_type(false),
'by_size' => new DirItemFilterer_by_size('600M', '>'),
'by_ext' => new DirItemFilterer_by_extension('_VIDEO_'),
// Use regular expressions to match years from 1900 to 2099
'by_name' => new DirItemFilterer_by_filename('[^0-9](19|20)[0-9]{2}[^0-9]')
)));
}
}
Related blog posts:
|
|
|
|
|
|
|
|
|