Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts

    Rename all files in directory to lowercase

    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

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    If your on Linux here is a tested shell script.


    lowercase.sh
    Code:
    #!/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.
    PHP Code:
    $files scandir($directory);
    foreach(
    $files as $key=>$name){
    $oldName $name;
    $newName strtolower($name);
    rename("$directory/$oldName","$directory/$newName");


    -------
    Last edited by Len Whistler; 01-17-2010 at 08:29 AM.
    Leonard Whistler

  • #3
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts
    No, I'm not. Thanks very much for your effort though! Appreciated.

    PHP Code:
    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?

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •