0

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.

4
Contributors
11
Replies
13
Views
7 Years
Discussion Span
Last Post by ayesha789
0

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

-1

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/

0

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);
    }
}
0

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);
    }
}

?>
0

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 */
}

Edited by digital-ether: n/a

0

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;
	}
0

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";
	}


?>
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.