Skip to content

Instantly share code, notes, and snippets.

@rk
Created February 27, 2019 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rk/63743e5b2178b38fad7a6849c8a77967 to your computer and use it in GitHub Desktop.
Save rk/63743e5b2178b38fad7a6849c8a77967 to your computer and use it in GitHub Desktop.
Batch remove prefix from files in current folder
<?php
/**
* rename.php - A Quick and Dirty Prefix Removal Script
*
* This is a quick and dirty prefix removal script. It's meant to be customized quickly and easily, and the
* typical options (the pattern and desired extensions) are defined immediately below.
*
* Writting in 2019 by Robert Kosek <robert[NOSPAM]@woodst.com>
*
* To the extent possible under law, the author(s) have dedicated all copyright and related and
* neighboring rights to this software to the public domain worldwide. This software is
* distributed without any warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication along with this
* software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
const PATTERN = '/glyphicons-basic-\d{1,4}-/';
const EXTENSIONS = ['svg'];
/**
* Avoids using ECHO directly in the `func() or echo '';` form,
* which PHP dislikes.
*/
function info(string $message) {
echo $message . PHP_EOL;
}
// Basic, non-recursive folder iteration of the current directory.
$iter = new DirectoryIterator(__DIR__);
foreach ($iter as $finfo) {
// Skip dot directories
if ($finfo->isDot()) continue;
// Skip files that don't match our desired extensions
if (!in_array($finfo->getExtension(), EXTENSIONS, true)) continue;
$file = $finfo->getFilename();
if (preg_match(PATTERN, $file)) {
$new = preg_replace(PATTERN, '', $file);
rename(__DIR__ . '/' . $file, __DIR__ . '/' . $new)
or info("Error renaming ${file} to ${new}...");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment