Last active
December 24, 2015 15:09
-
-
Save AmyStephen/6817915 to your computer and use it in GitHub Desktop.
Quick and dirty rename of all files of one file extension to another (in this case, .html.php to .phtml)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$objects = new RecursiveIteratorIterator | |
(new RecursiveDirectoryIterator(__DIR__), | |
RecursiveIteratorIterator::SELF_FIRST); | |
foreach ($objects as $path => $fileObject) { | |
$use = true; | |
$file_name = ''; | |
$base_name = ''; | |
if (is_dir($fileObject)) { | |
if ($fileObject->getFileName() == '.' || $fileObject->getFileName() == '..') { | |
} else { | |
$path = $fileObject->getPathName(); | |
foreach (scandir($path) as $filename) { | |
if ($filename == '.' || $filename == '..') { | |
} else { | |
$newname = preg_replace('"\.html.php"', '.phtml', $filename); | |
rename($path . '/' . $filename, $path . '/' . $newname); | |
} | |
} | |
} | |
} | |
} |
Or if you have zmv
$ zmv -W '*.html.php' '*.phtml'
Also less indentation and code re-use...
<?php
function isIgnoredFile($filename) {
return $filename == '.' || $filename == '..';
}
foreach ($objects as $path => $fileObject) {
$use = true;
$file_name = '';
$base_name = '';
if (!is_dir($fileObject) && isIgnoredFile($fileObject->getFileName())) {
continue;
}
$path = $fileObject->getPathName();
foreach (scandir($path) as $filename) {
if (isIgnoredFile($filename)) {
continue;
}
$newname = preg_replace('"\.html.php"', '.phtml', $filename);
rename($path . '/' . $filename, $path . '/' . $newname);
}
}
Thanks again, @peterjmit
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from +bobthecow