dflow Posted December 16, 2008 Share Posted December 16, 2008 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? Quote Link to comment Share on other sites More sharing options...
bluesoul Posted December 16, 2008 Share Posted December 16, 2008 You may have to be a bit more specific about what you're wanting to do, if there's a pattern to the renaming, etc. Quote Link to comment Share on other sites More sharing options...
Maq Posted December 16, 2008 Share Posted December 16, 2008 What do you want to rename them to? A script for this would be quite simple depending on the format. Quote Link to comment Share on other sites More sharing options...
dflow Posted December 16, 2008 Author Share Posted December 16, 2008 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 Quote Link to comment Share on other sites More sharing options...
flyhoney Posted December 16, 2008 Share Posted December 16, 2008 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); } ?> Quote Link to comment Share on other sites More sharing options...
Brian W Posted December 16, 2008 Share Posted December 16, 2008 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. Quote Link to comment Share on other sites More sharing options...
dflow Posted December 17, 2008 Author Share Posted December 17, 2008 thanks ill try it out Quote Link to comment Share on other sites More sharing options...
dflow Posted February 4, 2009 Author Share Posted February 4, 2009 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 Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted February 4, 2009 Share Posted February 4, 2009 if u get that error you have to get a wooden stick() cock it back() and then swing it forwarnd( onto you windows box) this is untested. Quote Link to comment Share on other sites More sharing options...
dflow Posted February 4, 2009 Author Share Posted February 4, 2009 ok tried that it is WORKING!!!!! Not anyone else? Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted February 4, 2009 Share Posted February 4, 2009 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. Quote Link to comment Share on other sites More sharing options...
dflow Posted February 4, 2009 Author Share Posted February 4, 2009 ok changed path to localhost now i get Warning: opendir(http://localhost/dwhotel/images/331) [function.opendir]: failed to open dir: not implemented in C:\wamp\www\dwhotel\Rename_images.php on line 40 Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted February 4, 2009 Share Posted February 4, 2009 ok changed path to localhost now i get Warning: opendir(http://localhost/dwhotel/images/331) [function.opendir]: failed to open dir: not implemented in C:\wamp\www\dwhotel\Rename_images.php on line 40 is there a directory called 331 on your machine ? Quote Link to comment Share on other sites More sharing options...
dflow Posted February 9, 2009 Author Share Posted February 9, 2009 hi yes the directory is 331 the folder that holds the images Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted February 9, 2009 Share Posted February 9, 2009 put this in your browser and tell me what come up http://localhost/dwhotel/images/331 Quote Link to comment Share on other sites More sharing options...
dflow Posted February 10, 2009 Author Share Posted February 10, 2009 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 Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted February 10, 2009 Share Posted February 10, 2009 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 Quote Link to comment Share on other sites More sharing options...
dflow Posted February 12, 2009 Author Share Posted February 12, 2009 cheers Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.