using php, I want to rename file '1' to 'email.txt'

rgb192
rgb192 used Ask the Experts™
on
many folders not recursive

folderA/1
folder45/1
folderHello/1
anotherFolder/1

files are named '1' with no extension name

I want to rename file '1' to 'email.txt'
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Marco GasiFreelancer
Top Expert 2010
Commented:
Hi rgb.
You can try this untested code:

foreach (new DirectoryIterator('path/to/root') as $fileInfo) {
    if($fileInfo->isDot()) continue;
    if ($fileInfo->getFilename()  = '1')
    {
        rename($fileInfo->getFilename(), 'email.txt'); 
    }
}

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
This tested out correctly for me.  I'm not leaving a copy on my server because once it is run, it has done its job.  Put this in the web root, or whatever high-level directory you choose.  It will handle every directory below and including itself.

Before you run something like this, always backup all of your directories.  Take careful note of the warning on the rename() man page.

<?php // temp_rgb192.php
error_reporting(E_ALL);


// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28450646.html

// REF http://php.net/manual/en/class.recursivedirectoryiterator.php#85805
// REF http://www.php.net/manual/en/function.rename.php#97347

// PUT THIS SCRIPT IN THE WEB ROOT DIRECTORY


// THE FILE PATHS AND NAMES IN PLAY
$path = realpath(getcwd());
$newf = DIRECTORY_SEPARATOR . 'email.txt';
$file = DIRECTORY_SEPARATOR . '1';
$flag = strrev($file);

// A REGULAR EXPRESSION TO FIND THE FILE BY BASENAME
$regx
= '#'               // REGEX DELIMITER
. preg_quote($file) // THE SIGNAL STRING
. '$'               // AT END OF FILE NAME
. '#'               // REGEX DELIMITER
;

// FIND ALL PATHS
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

// ITERATE OVER THE OBJECTS
foreach($objs as $name => $obj)
{
    // REVERSE THE BASENAME AND COMPARE
    $test = strrev($name);
    $test = substr($test, 0, strlen($flag));
    if ($test == $flag)
    {
        echo PHP_EOL . $name;
        $newb = preg_replace($regx, $newf, $name);
        rename ($name, $newb);
    }
}

Open in new window

unless I'm missing something, this should be much simpler

<?php
rename("1","email.txt");
?>

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
@mankowitz: Yes, that is exactly what the script does, but it provides a generalized solution that does not need to be installed and run in every single directory you have on your server (some might have many directories).  Sometimes less is more and sometimes more is more. ;-)

Author

Commented:
I like Ray's script the best because it worked for my directories.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial