Hi Dear All
I have 1000s of JPGs Images.
Names like S-KHI-1233
Now I need To rename all the images and new name should be last 4 numeric digits like 1233.
For all 1000 JPGs I want to do this.
if image name is S-KHI-1964 then new name should be 1964
and if name S-LAK-1854 then new name should be 1854.

I've used this: http://www.1-4a.com/rename/

But you can just search google for file renamer if you want another one.

Use regular expression:

eg:

$name = 'S-KHI-1233';

$name = preg_replace("/[^0-9]/", '', $name);

But I have these JPGs in the folder name IMAGES.
How I can apply this code?

Hi Dear All
I have 1000s of JPGs Images.
Names like S-KHI-1233
Now I need To rename all the images and new name should be last 4 numeric digits like 1233.
For all 1000 JPGs I want to do this.
if image name is S-KHI-1964 then new name should be 1964
and if name S-LAK-1854 then new name should be 1854.

i am not sure. i think this is help ful to you. checkit once.
http://sourceforge.net/projects/batchfilerename/

But I have these JPGs in the folder name IMAGES.
How I can apply this code?

PHP has numerous ways to traverse directories. Probably the simplest is the glob() function. (PHP4.3+)

http://php.net/manual/en/function.glob.php

You can use it like this:

// get an array of all .jpg files
$files = glob("*.jpg");
foreach ($files as $filename) {
    // make sure we have a file
    if (is_file($filename)) {
        // remove non-digits
        $newname = preg_replace("/[^0-9]/", '', $filename);
        // rename file
        rename($filename, $newname);
    }
}

I have change this according to my path but its not doing any thing as my images are stored in simimage Folder in server.

<?

// get an array of all .jpg files
$files = glob($_SERVER['DOCUMENT_ROOT']."/simimage/.jpg");
foreach ($files as $filename) {
    // make sure we have a file
    if (is_file($filename)) {
        // remove non-digits
        $newname = preg_replace("/[^0-9]/", '', $filename);
        // rename file
        rename($filename, $newname);
    }
}

?>

I have change this according to my path but its not doing any thing as my images are stored in simimage Folder in server.

<?

// get an array of all .jpg files
$files = glob($_SERVER['DOCUMENT_ROOT']."/simimage/.jpg");
foreach ($files as $filename) {
    // make sure we have a file
    if (is_file($filename)) {
        // remove non-digits
        $newname = preg_replace("/[^0-9]/", '', $filename);
        // rename file
        rename($filename, $newname);
    }
}

?>

Try:

$files = glob($_SERVER['DOCUMENT_ROOT']."/simimage/*.jpg");

Also, I just noticed that glob returns false on error. So you may want to catch that:

$files = glob($_SERVER['DOCUMENT_ROOT']."/simimage/.jpg");
if ($files) {
   /* rename your files */

} else {
  /* do your error handling */
}

Code is not showing anything in filename variabe when i use *
but when i put name ALU N-RMB-8221 manually it shows path like
C:/xampp/htdocs/simimage/ALU N-RMB-8221.jpg

$dir=$_SERVER['DOCUMENT_ROOT'];
$files = glob($dir."/simimage/ALU N-RMB-8221.jpg");

foreach ($files as $filename) 
	{
	echo $filename;
	}

no-1111.jpg stored in simimage and its showing error .
Its also gives error when I use * inplace of no-1111

<?

// get an array of all .jpg files
$dir=$_SERVER['DOCUMENT_ROOT'];
$files = glob($dir."/simimage/no-1111.jpg");



// make sure we have a file   
   if (is_file($files))
   { 

	foreach ($files as $filename) 
	{
 
	    // remove non-digits
        $newname = preg_replace("/[^0-9]/", '', $filename);
		
        // rename file
        rename($filename, $newname.".jpg");
    }
	}

	else 
	{
	echo "error";
	}


?>

no-1111.jpg stored in simimage and its showing error .
Its also gives error when I use * inplace of no-1111

<?

// get an array of all .jpg files
$dir=$_SERVER['DOCUMENT_ROOT'];
$files = glob($dir."/simimage/no-1111.jpg");



// make sure we have a file   
   if (is_file($files))
   { 

	foreach ($files as $filename) 
	{
 
	    // remove non-digits
        $newname = preg_replace("/[^0-9]/", '', $filename);
		
        // rename file
        rename($filename, $newname.".jpg");
    }
	}

	else 
	{
	echo "error";
	}


?>

First make sure you're getting all the files:

// get an array of all .jpg files
$dir=$_SERVER['DOCUMENT_ROOT'];
$files = glob($dir."/simimage/*.jpg");

var_dump($files);

When you do var_dump() it should show you all the values in the $files array. See that you have all the jpeg files.

The thing is that your regular expression is not correct, since now you're using absolute paths.

So change it to:

$newname = preg_replace("/([\/\\\]+)([^0-9\/\\\]+)([0-9]+)\.jpg$/i", '\\1\\3.jpg', $filename);

The regular expression matches any non-digits following a / or \ (directory separator).
It then matches any digits following that, ending with .jpg. It replaces the string with only the digits and .jpg, hence removing the non-digits after the directory separator.

OK I will try.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.