PHP File Rename Script

September 26, 2009 | PHP, Time Savers, Tutorials | 0 comments

Ever downloaded an archive of files and all of them had file names about 200 characters wrong with the same string in each? It’s more common that you might think… If you look at any audio archives you may have acquired, the chances are that the file names will have the quality in it (192 or 128 etc..). Additionally if you have video files the resolution or quality might be present.

I, for one, don’t really care what the quality is in the file name and would prefer it to be readable. I spend a lot of time removing this extraneous data so decided to make a short PHP script to remove certain strings on mass.

See the following code which can be run from the command line using the following format:

php -f <filename> <text_to_remove> [int live_mode=0]



  [int live_mode=0]';
} else {
    $remove = $argv[1];

$live_mode = (int)$argv[2];

if ($dir = scandir($cwd)) {
    foreach ($dir as $file) {
        $path = $cwd . '/' . $file;

        if (!in_array($file, $ignore)) {
            if (!is_link($path) && !is_dir($path)) {
                if (strpos($file, $remove) !== false && $file != $_SERVER['SCRIPT_FILENAME'] && $file != $remove) {
                    $output .= 'Renamed "' . $path . '" to "' . $cwd . '/' . trim(str_replace($remove, '', $file)) . '"' . "\n";

                    if ($live_mode) {
                        $new_path = $cwd . '/' . trim(str_replace($remove, '', $file));
                        rename($path, $new_path);

    if ($output) {
        $output = ($live_mode ? 'LIVE':'TEST') . ' MODE' . "\n\n" . 'Text to remove from filenames was "' . $remove . '"' . "\n\n" . $output;
    } else {
        $output = 'There were no matches by your criteria: "' . $remove . '"';

    echo $output . "\n\n";



To download the code in file form click the following link: [download#13#size#nohits]

A Donate Button!


Submit a Comment

Your email address will not be published. Required fields are marked *

CommentLuv badge

Divi United

About this site and Sean Barton

Picture of Sean
Sean Barton is a Freelance PHP Website Developer in Crewe, Cheshire. He is a Wordpress and CMS/Framework specialist.
This site was set up in 2008 as a tutorial and scripting resource for the PHP language and Wordpress.
Find out more about Sean on the About Me page or use the Hire Me page to get in touch. For more information about Sean's work take a look at the Portfolio

Our Services

  • Wordpress plugin/theme development
  • Divi specialist
  • Ecommerce (Woocommerce, WPSC, Shopify, Magento)
  • PSD to Wordpress theme conversion (Responsive)
  • Website design work (Banners, Logos, Full Site, etc)
  • Website analysis (security, usability, SEO)
  • API Integrations (InfusionSoft, SalesForce, Ontraport, Customer Thermometer, etc..)
  • Wordpress consultancy & expert advice
  • Crisis support
  • Website hosting

The main services offered are Wordpress based although we do a great deal of technical programming for bespoke systems. From troubleshooting, extending frameworks, finding bugs to writing them from scratch.

Find out more by looking through our past projects or get a quote.

Be the first to hear about new products/updates!

This is a mailing list for those people interested in being told when we release a new product (Divi plugin or Theme).

We shall also use this list to let you know about product updates and releases.

You have Successfully Subscribed!