Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Here is a solution using php based on this answerthis answer. Create a file eg myrename.php with the following contents:

<?php
$test = 1;
if(!chdir('/tmp/mydir')) echo "failed chdir\n";
else if ($handle = opendir('.')) {
    while (false !== ($fileName = readdir($handle))) {
        $newName = preg_replace("/\.(jpg|gif|png)$/i","-img4sm.\\1",$fileName);
        if($newName!=$fileName){
            if($test){
                echo "$fileName -> $newName\n";
            }else{
                if(!rename($fileName, $newName))echo "failed $fileName -> $newName\n";
            }
        }
    }
    closedir($handle);
}
?>

replace /tmp/mydir by the path of your directory. Run the script with

php myrename.php

and it will print out the old filename and new filename for each matched file. If this is ok, change $test = 1; to $test = 0; and run it for real. Remember to backup the files somewhere first in case of problems.

Here is a solution using php based on this answer. Create a file eg myrename.php with the following contents:

<?php
$test = 1;
if(!chdir('/tmp/mydir')) echo "failed chdir\n";
else if ($handle = opendir('.')) {
    while (false !== ($fileName = readdir($handle))) {
        $newName = preg_replace("/\.(jpg|gif|png)$/i","-img4sm.\\1",$fileName);
        if($newName!=$fileName){
            if($test){
                echo "$fileName -> $newName\n";
            }else{
                if(!rename($fileName, $newName))echo "failed $fileName -> $newName\n";
            }
        }
    }
    closedir($handle);
}
?>

replace /tmp/mydir by the path of your directory. Run the script with

php myrename.php

and it will print out the old filename and new filename for each matched file. If this is ok, change $test = 1; to $test = 0; and run it for real. Remember to backup the files somewhere first in case of problems.

Here is a solution using php based on this answer. Create a file eg myrename.php with the following contents:

<?php
$test = 1;
if(!chdir('/tmp/mydir')) echo "failed chdir\n";
else if ($handle = opendir('.')) {
    while (false !== ($fileName = readdir($handle))) {
        $newName = preg_replace("/\.(jpg|gif|png)$/i","-img4sm.\\1",$fileName);
        if($newName!=$fileName){
            if($test){
                echo "$fileName -> $newName\n";
            }else{
                if(!rename($fileName, $newName))echo "failed $fileName -> $newName\n";
            }
        }
    }
    closedir($handle);
}
?>

replace /tmp/mydir by the path of your directory. Run the script with

php myrename.php

and it will print out the old filename and new filename for each matched file. If this is ok, change $test = 1; to $test = 0; and run it for real. Remember to backup the files somewhere first in case of problems.

Source Link
meuh
  • 54.7k
  • 2
  • 70
  • 138

Here is a solution using php based on this answer. Create a file eg myrename.php with the following contents:

<?php
$test = 1;
if(!chdir('/tmp/mydir')) echo "failed chdir\n";
else if ($handle = opendir('.')) {
    while (false !== ($fileName = readdir($handle))) {
        $newName = preg_replace("/\.(jpg|gif|png)$/i","-img4sm.\\1",$fileName);
        if($newName!=$fileName){
            if($test){
                echo "$fileName -> $newName\n";
            }else{
                if(!rename($fileName, $newName))echo "failed $fileName -> $newName\n";
            }
        }
    }
    closedir($handle);
}
?>

replace /tmp/mydir by the path of your directory. Run the script with

php myrename.php

and it will print out the old filename and new filename for each matched file. If this is ok, change $test = 1; to $test = 0; and run it for real. Remember to backup the files somewhere first in case of problems.