note 88081 added to function.rename
| From: | brent at brenthugh dot com | Date: | Fri, 09 Jan 2009 06:31:38 +0000 |
| Subject: | note 88081 added to function.rename | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148979@lists.php.net to get a copy of this message | ||
This is an update to the code provided by dev at islam-soft dot com, which renames all files in a
directory with a given extension to have a different extension.
I updated four things: Most important, added a check to make sure the extension matched ONLY the end
of the filename (the original caused me a number of headaches when it started matching things in the
middle of filenames instead of at the end where a proper extension belongs), 2nd added some feedback
about what is going on, if you choose verbose mode=TRUE, 3rd added the "testing" parameter
so you can do a test run before committing for real, 4th added an error message if a rename fails.
parameter 1 : the directory name
parameter 2 : the first extension which we want to replace
parameter 3 : the new extension of files
parameter 4 : verbose? (true/false)
parameter 5 : testing? (true/false)--if true, won't actually rename anything; if you do
verbose=TRUE and testing=TRUE you can see what will happen before committing to it with
testing=FALSE.
Return is the number of files renamed.
for a simple usage call the function :
changeext('my/directory', 'html', 'php', false, false);
This changes every file name with extension html into php in the directory my/directory
<?PHP
//changes files in $directory with extension $ext1 to have extension $ext2
//note that if file.ext2 already exists it will simply be over-written
function changeext($directory, $ext1, $ext2, $verbose = false, $testing=true) {
if ($verbose && $testing) { echo "Testing only . . . <br />";}
$num = 0;
if($curdir = opendir($directory)) {
if ($verbose) echo "Opening $directory . . .<br />";
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, '$' ensures the ext is only at they end of the filename
//I think the next two lines are unnecessary but they
//don't hurt anything so I'm leaving them
$fileHand = fopen($srcfile, 'r');
fclose($fileHand);
if (!$testing) {
$result=rename($srcfile, $dstfile );
if (!$result) echo "ERROR renaming $srcfile -> $dstfile<br />";
}
if ($verbose) echo "$srcfile -> $dstfile<br />";
}
if(is_dir($srcfile)) {
$num += changeext($srcfile, $ext1, $ext2, $verbose, $testing);
}
}
}
closedir($curdir);
}
return $num;
}
$testing=true;
$verbose=true;
//example usages, first run with $testing set to false, then change $testing to true to do them for
real:
//changeext('your/directory', 'php', 'html', $verbose, $testing);
//changeext('your/directory', 'html', 'php', $verbose, $testing);
----
Server IP: 208.69.120.35
Probable Submitter: 68.93.11.176
----
Manual Page -- http://www.php.net/manual/en/function.rename.php
Edit -- https://master.php.net/note/edit/88081
Del: integrated -- https://master.php.net/note/delete/88081/integrated
Del: useless -- https://master.php.net/note/delete/88081/useless
Del: bad code -- https://master.php.net/note/delete/88081/bad+code
Del: spam -- https://master.php.net/note/delete/88081/spam
Del: non-english -- https://master.php.net/note/delete/88081/non-english
Del: in docs -- https://master.php.net/note/delete/88081/in+docs
Del: other reasons-- https://master.php.net/note/delete/88081
Reject -- https://master.php.net/note/reject/88081
Search -- https://master.php.net/manage/user-notes.php