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:
$obj->setRenameRules(array(
"newExtension" => "txt",
"prefix" => "test_"
));
$obj->renameRules->setOptions(array(
"newExtension" => "txt",
"prefix" => "test_"
));
Or set individual options:
$obj->renameRules->setOption("prefix", "test_");
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 |
Used to set a new extension for the renamed files (could be a string or an array of file extensions).
$obj->setRenameRules(array(
...
"newExtension" => "txt"
...
));
Files Before Rename | Files After Rename |
---|---|
source_file.php | source_file.txt |
mypage.html | mypage.txt |
test_file.ini | test_file.txt |
resume.html | resume.txt |
$obj->setRenameRules(array(
...
"newExtension" => array("html" => "txt", "conf" => "ini")
...
));
Files Before Rename | Files After Rename |
---|---|
source_file.php | source_file.php |
mypage.html | mypage.txt |
configurations.conf | configurations.ini |
resume.html | resume.txt |
Used to append extra extension to the file name of the renamed files.
$obj->setRenameRules(array(
...
"extraExtension" => "txt"
...
));
Files Before Rename | Files After Rename |
---|---|
source_file.php | source_file.php.txt |
mypage.html | mypage.html.txt |
test_file.ini | test_file.html.ini |
resume.html | resume.html.txt |
Used to make the file extension uppercase.
$obj->setRenameRules(array(
...
"uppercaseExtension" => true
...
));
Files Before Rename | Files After Rename |
---|---|
source_file.php | source_file.PHP |
test_file.ini | test_file.INI |
resume.html | resume.HTML |
Used to make the file extension lowercase.
$obj->setRenameRules(array(
...
"lowercaseExtension" => true
...
));
Files Before Rename | Files After Rename |
---|---|
source_file.PHP | source_file.php |
test_file.INI | test_file.ini |
resume.HTML | resume.html |
Used to remove specific characters from the file name.
$obj->setRenameRules(array(
...
"removeChars" => array("_", "&", "$")
...
));
Files Before Rename | Files After Rename |
---|---|
file_name.html | filename.html |
track$15.mp3 | track15.mp3 |
&test_page.html | testpage.html |
Used to remove specific words from the file name.
$obj->setRenameRules(array(
...
"removeWords" => array("name", "test_")
...
));
Files Before Rename | Files After Rename |
---|---|
filename.html | file.html |
track15.mp3 | track15.mp3 |
test_page.html | page.html |
Used to remove the first N of characters from the file name.
$obj->setRenameRules(array(
...
"removeFromLeft" => 5
...
));
Files Before Rename | Files After Rename |
---|---|
file_name.html | name.html |
track15.mp3 | 15.mp3 |
test_page.html | page.html |
Used to remove the last N of characters from the file name.
$obj->setRenameRules(array(
...
"removeFromRight" => 2
...
));
Files Before Rename | Files After Rename |
---|---|
file_name_2.html | file_name.html |
track15.mp3 | track.mp3 |
page_list.html | page_li.html |
Used to remove a specific part from the file name indicated by the starting position and the length that will be removed.
$obj->setRenameRules(array(
...
"removeLength" => array(3, 5)
...
));
Files Before Rename | Files After Rename |
---|---|
idx_name(1).txt | idx(1).txt |
idx_test(2).txt | idx(2).txt |
idx_file(3).txt | idx(3).txt |
Used to add a prefix to the file name.
$obj->setRenameRules(array(
...
"prefix" => "img_"
...
));
Files Before Rename | Files After Rename |
---|---|
dog.jpeg | img_dog.jpeg |
cat.png | img_cat.png |
fox.png | img_fox.png |
Used to add a suffix to the file name.
$obj->setRenameRules(array(
...
"suffix" => "_img"
...
));
Files Before Rename | Files After Rename |
---|---|
dog.jpeg | dog_img.jpeg |
cat.png | cat_img.png |
fox.png | fox_img.png |
Used to insert a text to the file name at specific position.
$obj->setRenameRules(array(
...
"insert" => array("_own_", 2)
...
));
Files Before Rename | Files After Rename |
---|---|
myresume.html | my_own_resume.html |
mylink.txt | my_own_link.txt |
myfile.txt | my_own_file.txt |
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:
$obj->setRenameRules(array(
...
"replace" => array("name", "title")
...
));
Files Before Rename | Files After Rename |
---|---|
filename.txt | filetitle.txt |
name_12.txt | title_12.txt |
pagename.html | pagetitle.html |
Used to perform a regular expression search and replace within the file name.
This option takes an array of two values:
$obj->setRenameRules(array(
...
'regex' => array('/filename(\d+)/i', 'myfile_${1}')
...
));
Files Before Rename | Files After Rename |
---|---|
filename1.txt | myfile_1.txt |
filename15.txt | myfile_15.txt |
filenametest.html | filenametest.html |
Used to make the file name to uppercase.
$obj->setRenameRules(array(
...
"toUppercase" => true
...
));
Files Before Rename | Files After Rename |
---|---|
filename.jpeg | FILENAME.JPEG |
readme.txt | README.TXT |
Used to make the file name to lowercase.
$obj->setRenameRules(array(
...
"toLowercase" => true
...
));
Files Before Rename | Files After Rename |
---|---|
FileName.JPEG | filename.jpeg |
README.txt | readme.txt |
Used to convert the file name to camelCase.
$obj->setRenameRules(array(
...
"toCamelCase" => true
...
));
Files Before Rename | Files After Rename |
---|---|
the file_name.JPEG | theFileName.jpeg |
image-name.txt | imageName.txt |
Used to capitalize the first character of the file name.
$obj->setRenameRules(array(
...
"capitalizeFirst" => true
...
));
Files Before Rename | Files After Rename |
---|---|
filename.txt | Filename.txt |
dog.jpeg | Dog.jpeg |
image.jpeg | Image.jpeg |
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:
/**
* 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"
...
));
Files Before Rename | Files After Rename |
---|---|
notes.inc | notes.html |
readme.txt | readme.html |
functions.inc | functions.php |
cat.jpeg | cat.jpeg |
globals.inc | globals.php |