BatchFileRename Libraryv1.0

Filename Variables

Filename Variables is a powerfull feature tends to automate the renaming process. It can be used in several ways for example you can automatically auto-number a set of files eg. track-1.mp3, track-2.mp3, track-3.mp3. or you can add a text to the filename based on the file extension.


How it works

And here is a list of renaming rules that supports filename variables:


Core Filename Variables

BatchFileRename comes with some pre-set filename variables wich is:


Usage Examples



Usage Examples

Auto-numbering

Code:
// Include BatchFileRename class file
require ("path/to/BatchFileRename.php");

// Create a new class instance
$obj = new BatchFileRename();

// Define the fileSet filters
$obj->setFileSetOptions(array(
    
"directoryPath" => "/path/to/directory/"
    
"includeExtensions" => array("mp3")
    ));

// Define the renaming rules
$obj->setRenameRules(array(
    
"prefix" => "<##> - Coldplay - " 
    
));

// Rename the files
$obj->rename();
Result:
This will auto-number all the mp3 files within the directory.
Files Before RenameFiles After Rename
Cemeteries Of London.mp301 - Coldplay - Cemeteries Of London.mp3
Death And All His Friends.mp302 - Coldplay - Death And All His Friends.mp3
Lost.mp303 - Coldplay - Lost.mp3
The Escapist.mp304 - Coldplay - The Escapist.mp3
Viva La Vida.mp305 - Coldplay - Viva La Vida.mp3

Using a fixed filename

Code:
// Include BatchFileRename class file
require ("path/to/BatchFileRename.php");

// Create a new class instance
$obj = new BatchFileRename();

// Define the fileSet filters
$obj->setFileSetOptions(array(
    
"directoryPath" => "/path/to/directory/"
    
"includeExtensions" => array("mp3")
    ));

// Define the renaming rules
$obj->setRenameRules(array(
    
"fixedFileName" => "track-<##>" 
    
));

// Rename the files
$obj->rename();
Result:
This will rename all the mp3 files within the directory to a fixed filename "track-(n).mp3".
Files Before RenameFiles After Rename
Cemeteries Of London.mp3track-01.mp3
Death And All His Friends.mp3track-02.mp3
Lost.mp3track-03.mp3
The Escapist.mp3track-04.mp3
Viva La Vida.mp3track-05.mp3

Using Multiple Filename Variables

This example shows the possibility to use multiple filename variables can be use at the same time.

Code:
// Include BatchFileRename class file
require ("path/to/BatchFileRename.php");

// Create a new class instance
$obj = new BatchFileRename();

// Define the fileSet filters
$obj->setFileSetOptions(array(
    
"directoryPath" => "/path/to/directory/"
    
"includeExtensions" => array("mp3")
    ));

function 
artistFileNameVariable($fileinfo$renameinfo) {
    if(
$fileinfo->basename == "Lost" || $fileinfo->basename == "The Escapist"){
        
retutn "Enrique Iglesias";
    }
    
    return 
"Coldplay";
}

// Define the Filename Variables "artist" and its callback.
$obj->setFileNameVariable("artist""artistFileNameVariable");

// Define the renaming rules
$obj->setRenameRules(array(
    
"prefix" => "<##> - <artist> - ",
    ));

// Rename the files
$obj->rename();
Result:
Files Before RenameFiles After Rename
Cemeteries Of London.mp301 - Coldplay - Cemeteries Of London.mp3
Death And All His Friends.mp302 - Coldplay - Death And All His Friends.mp3
Lost.mp303 - Enrique Iglesias - Lost.mp3
The Escapist.mp305 - Enrique Iglesias - The Escapist.mp3
Viva La Vida.mp306 - Coldplay - Viva La Vida.mp3

Using getID3() to rename Multiple mp3 files

This example shows the possibility to use getID3() library to rename a set of mp3 files according to their id3 metadata.

Code:
// Include BatchFileRename class file
require ("path/to/BatchFileRename.php");

// Include getID3 library
require ("getid3/getid3.php");

// Create a new class instance
$obj = new BatchFileRename();

// Define the fileSet filters
$obj->setFileSetOptions(array(
    
"directoryPath" => "/path/to/directory/"
    
"includeExtensions" => array("mp3")
    ));

function 
get_id3_metadata($filename$tag) {

    static 
$getID3$files;
    
    if (!isset(
$getID3)) {
        
$getID3 = new getID3();
    }
    
    if (!isset(
$files)) {
        
$files = array();
    }
    
    if (!isset(
$files[$filename])) {
        
$fileinfo $getID3->analyze($filename);
        
getid3_lib::CopyTagsToComments($fileinfo);
        
$files[$filename] = $fileinfo;
    }
    
    if(!isset(
$files[$filename]["comments"][$tag][0])){
        return 
NULL;
    }
    
    return 
$files[$filename]["comments"][$tag][0];
}

function 
artistFileNameVariable($fileinfo$renameinfo) {
    
    
$id3_artist get_id3_metadata($fileinfo->path"artist");

    if (empty(
$id3_artist)) {
        return 
"Unknown";
    }
    
    return 
$id3_artist;
}

function 
titleFileNameVariable($fileinfo$renameinfo) {
    
    
$id3_title get_id3_metadata($fileinfo->path"title");

    if (empty(
$id3_title)) {
        return 
$fileinfo->basename;
    }
    
    return 
$id3_title;
}

// Define the Filename Variables "title", "artist" and their callbacks.
$obj->setFileNameVariable("title""titleFileNameVariable");
$obj->setFileNameVariable("artist""artistFileNameVariable");

// Define the renaming rules
$obj->setRenameRules(array(
    
"fixedFileName" => "<##> - <artist> - <title>",
    ));

// Rename the files
$obj->rename();
Result:
Files Before RenameFiles After Rename
04 - Charlie Brown.mp301 - Coldplay - Charlie Brown.mp3
04-50_cent-dont_push_me_(feat_eminem_and_lloyd_banks)-rns.mp302 - 50 Cent - Don't Push Me (feat. Eminem & Lloyd Banks).mp3
07 - Nelly feat. Jermaine Dupri, P - Grill.mp303 - Nelly feat. Jermaine Dupri, P - Grill.mp3
10 - Princess of China.mp304 - Coldplay - Princess of China.mp3
12 - A Hopeful Transmission.mp305 - Coldplay - A Hopeful Transmission.mp3
Hey Baby (Drop It To The Floor).mp306 - Pitbull & T-Pain - Hey Baby (Drop It to the Floor).mp3
If You Seek Amy-Britney Spears.mp307 - Britney Spears - If You Seek Amy.mp3
Katty_Perry_-_I_kissed_a_girl.mp308 - Katy Perry - I Kissed A Girl.mp3
Kat_DeLuna_-_In_The_End.mp309 - Kat DeLuna - In The End.mp3
Rihana - Unfaithful.mp310 - Rihana - Unfaithful.mp3