I'm currently using the following code to list all of the subdirectories within a specific directory.
$dir = realpath('custom_design_copy/');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
if(is_dir($object)){
echo "$name<br />";
}
}
This gives me results that look something like this.
C:\Data\Web\custom_design_copy\Product
C:\Data\Web\custom_design_copy\Product\images
C:\Data\Web\custom_design_copy\Product\Scripts
What I want to do is rename all of these subdirectories with strtoupper()
in order to normalize all of their names. I'm aware of the rename()
function but I fear that if I try the following:
rename($name, strtoupper($name));
I'll wind up modifying one of custom_design_copy's parent directory names, which is what I hope to avoid. How can I avoid this issue? I was considering using substr()
and searching for the last occurrence of "\" in order to isolate the directory name at the end, but there has to be an easier solution to this. The end result I'm looking for is this.
C:\Data\Web\custom_design_copy\PRODUCT
C:\Data\Web\custom_design_copy\PRODUCT\IMAGES
C:\Data\Web\custom_design_copy\PRODUCT\SCRIPTS
EDIT: While waiting for advice, I attempted my initial approach and found that it worked.
$dir = realpath('custom_design_copy/');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
if(is_dir($object)){
$slashPos = strrpos($name, '\\');
$newName = substr($name, 0, $slashPos + 1) . strtoupper(substr($name, $slashPos + 1));
rename($name, $newName);
echo "$name<br />";
}
}