Jump to content

BULK image naming


dflow

Recommended Posts

anyone have an idea how you can rename a bulk of images

when they in this format:

 

folder1

        image1

        image2

 

folder2

        image1

        image2

 

is there a tool

or

how would one approach writing a script for this?

 

 

Link to comment
Share on other sites

ok here is the logic:

 

i have a folder with 2000 images named called images:

inside each folder i have a product folder contain images

 

IMAGES

          Product_1

                      image_1.jpg

                      image_2.jpg

 

 

what i want to do is rename the image file as:

Product_1_image_1.jpg (get the foldername(which is the product name))

Product_1_image_2.jpg

 

Product_2_image_1.jpg

Product_2_image_2.jpg

 

and SAVE them to a NEW FOLDER:

NEW_IMAGES

 

Link to comment
Share on other sites

This is untested, but you can use it as a guide:

 

<?php
$old_dir = 'IMAGES';
$new_dir = 'NEW_IMAGES';

if ($product_handle = opendir($old_dir)) 
{
while (($product = readdir($product_handle)) !== false) 
{
	if ($product != "." && $product != "..") 
	{
		echo "product: $product\n";
		if ($image_handle = opendir($product)) 
		{
			while (($image = readdir($image_handle)) !== false) 
			{
				if ($image != "." && $image != "..") 
				{
					echo "image: $image\n";
					$old_image = $old_dir . '/' . $product . '/' . $image;
					$new_image = $new_dir . '/' . $product . '_' . $image;

					if (!copy($old_image, $new_image)) 
					{
						echo "failed to copy $old_image...\n";
					}
					else 
					{
						echo "copied $old_image to $new_image...\n";
					}
				}
			}
			closedir($image_handle);
		}
	}
}
closedir($product_handle);
}
?>

Link to comment
Share on other sites

irfanview does it pretty nicely, just make sure you make a backup of the pictures first no matter what tool you use. I've heard horor stories about batch file modifications.  :)      safe practice anyways

 

irfanview also resize and get rid of red eye easily too. Its no photoshop or gimpshop, but its very handy and I even use it as my all around image viewer.

Link to comment
Share on other sites

  • 1 month later...

ok took me some time to test it

 

i get this error:

product: apartment_331_1b.jpg

Warning: opendir(apartment_331_1b.jpg) [function.opendir]: failed to open dir: No error in C:\wamp\www\dwhotel\Rename_images.php on line 47

 

for all the files in the folder

this is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RENAME IMAGE</title>
</head>

<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table width="400" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <th scope="col"><label>
        <div align="center">
          <input type="text" name="old_dir" id="old_dir" />
          </div>
      </label></th>
    </tr>
    <tr>
      <td><label>
        <div align="center">
          <input type="text" name="new_dir" id="new_dir" />
          </div>
      </label></td>
    </tr>
    <tr>
      <td><label>
        <div align="center">
          <input type="submit" name="button" id="button" value="Submit" />
          </div>
      </label></td>
    </tr>
  </table>
</form>
</body>

<?php
$old_dir = $_POST['old_dir'];
$new_dir = $_POST['new_dir'];

if ($product_handle = opendir($old_dir)) 
{
   while (($product = readdir($product_handle)) !== false) 
   {
      if ($product != "." && $product != "..") 
      {
         echo "product: $product\n";
         if ($image_handle = opendir($product)) 
         {
            while (($image = readdir($image_handle)) !== false) 
            {
               if ($image != "." && $image != "..") 
               {
                  echo "image: $image\n";
                  $old_image = $old_dir . '/' . $product . '/' . $image;
                  $new_image = $new_dir . '/' . $product . '_' . $image;
                  
                  if (!copy($old_image, $new_image)) 
                  {
                     echo "failed to copy $old_image...\n";
                  }
                  else 
                  {
                     echo "copied $old_image to $new_image...\n";
                  }
               }
            }
            closedir($image_handle);
         }
      }
   }
   closedir($product_handle);
}
?>

</body>
</html>

 

this is the path to the old_dir:

C:\Documents and Settings\Dflow\Desktop\rome(2)\Rome\331

this is the path to the new_dir:

C:\Documents and Settings\Dflow\Desktop\rome(2)\Rome\331\Renamed

 

your help

 

thanks

Link to comment
Share on other sites

if the path is right then the permissions are wrong, more than likely the path is wrong, i am sure its wrong

 

opendir(apartment_331_1b.jpg)

 

 

that does sound wrong, do some reading man, make it work proper, otherwise your just asking for the entire solution you might aswell give us the job.

Link to comment
Share on other sites

put this in your browser and tell me what come up

 

http://localhost/dwhotel/images/331

 

i get:

Index of /dwhotel/images/331

 

    * Parent Directory

    * Thumbs.db

    * apartment_331_1b.jpg

    * apartment_331_2b.jpg

    * apartment_331_3b.jpg

    * apartment_331_4.jpg

    * apartment_331_4b.jpg

    * apartment_331_big.jpg

    * apartment_331_in.jpg

    * apartment_331_out.jpg

    * apartment_331_xbig.jpg

 

Link to comment
Share on other sites

basicaly i advise you to start from scratch, we dont whant to waste each others time.

 

this is what you do

 

read the names of all the directories into an array, if you want select directories then only specify them

 

then for each directory name in that array {

   

  rewind directory handle

 

   using the directory name read all the files names into an array 

 

   for each file in the array {

         

          get the file using its name in this array

          rename the file how you like

   }

 

}

 

 

 

here is the manual

 

http://uk.php.net/manual/en/book.dir.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.