...

View Full Version : Rename all files in directory to lowercase



nobackseat88
01-17-2010, 08:14 AM
Hello,

I have a list of images in my directory that are in link with my database, and are case sensitive. Some of them (well a vast majority) have uppercase letters, and I need them all to be lowercase. Can somebody start the script off for me of how I could go about doing this?

Thanks much,
nobackseat

Len Whistler
01-17-2010, 08:22 AM
If your on Linux here is a tested shell script.


lowercase.sh


#!/bin/bash

for f in *; do
g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
mv "$f" "$g"
done


Here is untested php script.

$files = scandir($directory);
foreach($files as $key=>$name){
$oldName = $name;
$newName = strtolower($name);
rename("$directory/$oldName","$directory/$newName");
}


-------

nobackseat88
01-17-2010, 08:27 AM
:( No, I'm not. Thanks very much for your effort though! Appreciated.



function dirList ($directory)
{

// create an array to hold directory list
$results = array();

// create a handler for the directory
$handler = opendir($directory);

// keep going until all files in directory have been read
while ($file = readdir($handler)) {

// if $file isn't this directory or its parent,
// add it to the results array
if ($file != '.' && $file != '..')
$results[] = $file;
}

// tidy up: close the handler
closedir($handler);

// done!
return $results;

}
$arr = dirList("images");
foreach ($arr as $key => $value) {
$value2 = strtolower($value);
rename($value,$value2);
}


Perhaps something like that? Anybody got any better ideas?

kbluhm
01-17-2010, 09:50 AM
Len's second example would work fine for you. It is essentially identical to what you've come up with in your example above, the only difference being that unnecessary dirList() function.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum