Skip to content

Instantly share code, notes, and snippets.

@AmyStephen
Last active December 24, 2015 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AmyStephen/6817915 to your computer and use it in GitHub Desktop.
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)
<?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);
}
}
}
}
}
@AmyStephen
Copy link
Author

from +bobthecow

<?php 

for f in *.html.php; do mv "\$f" "\${f%.html.php}.phtml"; 

@peterjmit
Copy link

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);
    }
}

@AmyStephen
Copy link
Author

Thanks again, @peterjmit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment