note 72164 deleted from function.rename by nlopess
| From: | nlopess@php.net | Date: | Fri, 05 Jan 2007 13:03:42 +0000 |
| Subject: | note 72164 deleted from function.rename by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-121316@lists.php.net to get a copy of this message | ||
Note Submitter: Mohamed Almasry (dev at islam-soft dot com)
----
- rename extension of files
changeext($directory, $ext1, $ext2, $verbose)
i wrote this function to rename the extention of some files in a folder and sub-folders inside it ..
parameter 1 : the directory name
parameter 2 : the first extention wich we want to replace
parameter 3 : the new extention of files
for a simple usage call the function :
changeext('dir', 'html', 'php', 'false');
to change evry file name with extention html into php in the directory dir
<?php
function changeext($directory, $ext1, $ext2, $verbose = false) {
$num = 0;
if($curdir = opendir($directory)) {
while($file = readdir($curdir)) {
if($file != '.' && $file != '..') {
$srcfile = $directory . '/' . $file;
$string = "$file";
$str = strlen($ext1);
$str++;
$newfile = substr($string, 0, -$str);
$newfile = $newfile.'.'.$ext2;
$dstfile = $directory . '/' . $newfile;
if (eregi("\.$ext1",$file)) { # Look at only files with a pre-defined extension
$fileHand = fopen($srcfile, 'r');
fclose($fileHand);
rename($srcfile, $dstfile );
}
if(is_dir($srcfile)) {
$num += changeext($srcfile, $ext1, $ext2, $verbose);
}
}
}
closedir($curdir);
}
return $num;
}
changeext('dir', 'html', 'php', 'false');
?>
to remove the extention of files , just leave the parameter $ext2 blank ''
devloped by : Mohamed Almasry - Islam Soft - Cairo
www.islam-soft.com