BatchFileRename Libraryv1.0

Renaming Rules

RenameRules is set of rules designated to define criteria for renaming files. In another way RenameRules is used to set the new filename. RenameRules options can be defined in two ways:

Options Table
Option Type Description Usage
fixedFileName string Used to set a fixed filename pattern for all renamed files.
Note: This option must be used in combination with Filename Variables in order not to get the same filename for all files.
Usage
newExtension string|array Used to set a new extension for the renamed files (could be a string or an array of file extensions). Usage
extraExtension string Used to append extra extension to the file name of the renamed files. Usage
uppercaseExtension boolean Used to make the file extension uppercase. Usage
lowercaseExtension boolean Used to make the file extension lowercase. Usage
removeChars array Used to remove specific characters from the file name. Usage
removeWords array Used to remove specific words from the file name. Usage
removeFromLeft integer Used to remove the first N of characters from the file name. Usage
removeFromRight integer Used to remove the last N of characters from the file name. Usage
removeLength array Used to remove a specific part from the file name indicated by the starting position and the length that will be removed. Usage
prefix string Used to add a prefix to the file name. Usage
suffix string Used to add a suffix to the file name. Usage
insert array Used to insert a text to the file name at specific position. Usage
replace array Used to replace all occurrences of the search string with the replacement string wihthin the file name. Usage
ireplace array Case-insensitive version of the previous option "replace".
regex array Used to perform a regular expression search and replace within the file name. Usage
toUppercase boolean Used to make the file name to uppercase. Usage
toLowercase boolean Used to make the file name to lowercase. Usage
toCamelCase boolean Used to convert the file name to camelCase. Usage
capitalizeFirst boolean Used to capitalize the first character of the file name. Usage
callback callback Used to set the new file name using a user-defined callback, the callback must return a non-empty string value, otherwise the file will not be renamed. Usage

Options Usage Examples

newExtension

Used to set a new extension for the renamed files (could be a string or an array of file extensions).

Code:
$obj->setRenameRules(array(
    ...
    
"newExtension" => "txt" 
    
...
    ));
Result:
Files Before RenameFiles After Rename
source_file.phpsource_file.txt
mypage.htmlmypage.txt
test_file.initest_file.txt
resume.htmlresume.txt
Code:
$obj->setRenameRules(array(
    ...
    
"newExtension" => array("html" => "txt""conf" => "ini")
    ...
    ));
Result:
Files Before RenameFiles After Rename
source_file.phpsource_file.php
mypage.htmlmypage.txt
configurations.confconfigurations.ini
resume.htmlresume.txt

extraExtension

Used to append extra extension to the file name of the renamed files.

Code:
$obj->setRenameRules(array(
    ...
    
"extraExtension" => "txt" 
    
...
    ));
Result:
Files Before RenameFiles After Rename
source_file.phpsource_file.php.txt
mypage.htmlmypage.html.txt
test_file.initest_file.html.ini
resume.htmlresume.html.txt

uppercaseExtension

Used to make the file extension uppercase.

Code:
$obj->setRenameRules(array(
    ...
    
"uppercaseExtension" => true
    
...
    ));
Result:
Files Before RenameFiles After Rename
source_file.phpsource_file.PHP
test_file.initest_file.INI
resume.htmlresume.HTML

lowercaseExtension

Used to make the file extension lowercase.

Code:
$obj->setRenameRules(array(
    ...
    
"lowercaseExtension" => true
    
...
    ));
Result:
Files Before RenameFiles After Rename
source_file.PHPsource_file.php
test_file.INItest_file.ini
resume.HTMLresume.html

removeChars

Used to remove specific characters from the file name.

Code:
$obj->setRenameRules(array(
    ...
    
"removeChars" => array("_""&""$")
    ...
    ));
Result:
Files Before RenameFiles After Rename
file_name.htmlfilename.html
track$15.mp3track15.mp3
&test_page.htmltestpage.html

removeWords

Used to remove specific words from the file name.

Code:
$obj->setRenameRules(array(
    ...
    
"removeWords" => array("name""test_")
    ...
    ));
Result:
Files Before RenameFiles After Rename
filename.htmlfile.html
track15.mp3track15.mp3
test_page.htmlpage.html

removeFromLeft

Used to remove the first N of characters from the file name.

Code:
$obj->setRenameRules(array(
    ...
    
"removeFromLeft" => 5
    
...
    ));
Result:
Files Before RenameFiles After Rename
file_name.htmlname.html
track15.mp315.mp3
test_page.htmlpage.html

removeFromRight

Used to remove the last N of characters from the file name.

Code:
$obj->setRenameRules(array(
    ...
    
"removeFromRight" => 2
    
...
    ));
Result:
Files Before RenameFiles After Rename
file_name_2.htmlfile_name.html
track15.mp3track.mp3
page_list.htmlpage_li.html

removeLength

Used to remove a specific part from the file name indicated by the starting position and the length that will be removed.

Code:
$obj->setRenameRules(array(
    ...
    
"removeLength" => array(35)
    ...
    ));
Result:
Files Before RenameFiles After Rename
idx_name(1).txtidx(1).txt
idx_test(2).txtidx(2).txt
idx_file(3).txtidx(3).txt

prefix

Used to add a prefix to the file name.

Code:
$obj->setRenameRules(array(
    ...
    
"prefix" => "img_"
    
...
    ));
Result:
Files Before RenameFiles After Rename
dog.jpegimg_dog.jpeg
cat.pngimg_cat.png
fox.pngimg_fox.png

suffix

Used to add a suffix to the file name.

Code:
$obj->setRenameRules(array(
    ...
    
"suffix" => "_img"
    
...
    ));
Result:
Files Before RenameFiles After Rename
dog.jpegdog_img.jpeg
cat.pngcat_img.png
fox.pngfox_img.png

insert

Used to insert a text to the file name at specific position.

Code:
$obj->setRenameRules(array(
    ...
    
"insert" => array("_own_"2)
    ...
    ));
Result:
Files Before RenameFiles After Rename
myresume.htmlmy_own_resume.html
mylink.txtmy_own_link.txt
myfile.txtmy_own_file.txt

replace

Used to replace all occurrences of the search string with the replacement string wihthin the file name.

This option takes an array of two values:

Code:
$obj->setRenameRules(array(
    ...
    
"replace" => array("name""title")
    ...
    ));
Result:
Files Before RenameFiles After Rename
filename.txtfiletitle.txt
name_12.txttitle_12.txt
pagename.htmlpagetitle.html

regex

Used to perform a regular expression search and replace within the file name.

This option takes an array of two values:

Code:
$obj->setRenameRules(array(
    ...
    
'regex' => array('/filename(\d+)/i''myfile_${1}')
    ...
    ));
Result:
Files Before RenameFiles After Rename
filename1.txtmyfile_1.txt
filename15.txtmyfile_15.txt
filenametest.htmlfilenametest.html

toUppercase

Used to make the file name to uppercase.

Code:
$obj->setRenameRules(array(
    ...
    
"toUppercase" => true
    
...
    ));
Result:
Files Before RenameFiles After Rename
filename.jpegFILENAME.JPEG
readme.txtREADME.TXT

toLowercase

Used to make the file name to lowercase.

Code:
$obj->setRenameRules(array(
    ...
    
"toLowercase" => true
    
...
    ));
Result:
Files Before RenameFiles After Rename
FileName.JPEGfilename.jpeg
README.txtreadme.txt

toCamelCase

Used to convert the file name to camelCase.

Code:
$obj->setRenameRules(array(
    ...
    
"toCamelCase" => true
    
...
    ));
Result:
Files Before RenameFiles After Rename
the file_name.JPEGtheFileName.jpeg
image-name.txtimageName.txt

capitalizeFirst

Used to capitalize the first character of the file name.

Code:
$obj->setRenameRules(array(
    ...
    
"capitalizeFirst" => true
    
...
    ));
Result:
Files Before RenameFiles After Rename
filename.txtFilename.txt
dog.jpegDog.jpeg
image.jpegImage.jpeg

callback

Used to set the new file name using a user-defined callback, the callback must return a non-empty string value, otherwise the file will not be renamed. .

The callback takes only two paramaters:

Code:
/**
 * A custom callback for renaming TXT and INC files only.
 *
 * The $fileinfo paramater contains file informations:
 *  $fileinfo->path         The file path
 *  $fileinfo->name         The file name
 *  $fileinfo->basename     The file name without the extension
 *  $fileinfo->extension    The file extension
 *  $fileinfo->size         The file size
 *
 * The $renameinfo paramater contains renamed file informations:
 *  $renameinfo->name       The new file name
 *  $renameinfo->basename   The new file name without the extension
 *  $renameinfo->extension  The new file extension
 *
 * @param   object  $fileinfo   Object containing file information
 * @param   object  $renameinfo Object containing renamed file information
 * @return  mixed   If string, it will be used as the new filname, otherwise the file will not be renamed
 */
function myCustomCallback($fileinfo$renameinfo){

    if(
$fileinfo->extension == "txt"){
        return 
$renameinfo->basename.".html";
    }

    if(
$fileinfo->extension == "inc"){
        return 
$renameinfo->basename.".php";
    }
    
    return 
false;
}

$obj->setRenameRules(array(
    ...
    
"callback" => "myCustomCallback"
    
...
    ));
Result:
The callback function will rename only TXT files to HTML files and INC files to PHP files.
Files Before RenameFiles After Rename
notes.incnotes.html
readme.txtreadme.html
functions.incfunctions.php
cat.jpegcat.jpeg
globals.incglobals.php