Delete One Image from Group and Rename the Rest Appropriately

#1

Here is a question. I want to be able to delete select image files, but rename the rest in a loop. The reason I do this is because each item I have is told that it has a certain number of images, that’s it. So, for an example, let’s say the item is a T-shirt, and I have the image name set to “tshirt” and I tell the item it has 5 images. It knows to grab the images like so:

tshirt.jpg
tshirt-2.jpg
tshirt-3.jpg
tshirt-4.jpg
tshirt-5.jpg

Now, let’s say I want to drop the number of images to 4, and delete tshirt-3.jpg. What’s the best way to rename the images after to move them down one? So, rename tshirt-4.jpg to tshirt-3.jpg, and tshirt-5.jpg to tshirt-4.jpg.

Any feedback appreciated.

Cheers!
Ryan

#2

A little bit of the PHP script you have tried to help users point you in the right direction would be helpful.

#3

Sorry about that, will post some tonight. I haven’t given a full attempt just yet as I don’t know where to start exactly. I have a script the runs a loop to double-check count all the files by filename, so was thinking of getting total number of files, creating an array with filenames, removing the filename that is about to be deleted, then run a loop that goes through the array and renames them all.

1 Like
#4

“Begin at the beginning,” the King said, very gravely, “and go on till you come to the end; then stop.”
-Lewis Carroll, Alice in Wonderland

You say you don’t know where to start, and then tell us you’ve got a script that follows logic. So you do know where to start; by defining what you want the script to do.

So it sounds like you already have your script planned. What exactly do you need help with?

#5

I think, you walk a wrong way. Call any image dependently of some unique param, like tshirt-nike-cat-2856G. That solves your problem.

#6

I was just seeing if there was a better way or function. Instead of a loop, just to run a wildcard on the image name (grab all files by wildcard), delete one, and then loop.

Right now I’d be running loops based on what the database says, instead of the file system.

#7

In your original post it looks as though their is an image folder with images prefixed with “t-shirt???.jpg”. Using Php it is easy to fill an array with all these t-shirt images using the following function:

https://www.php.net/manual/en/function.glob.php

I am not sure which images require deleting but I would prefer to rename an image rather than remove completely, far easier than having to upload, etc on the off-chance it may be required at a later date.

#8

That’s a good point, to rename instead of delete. Will consider.

#9

As John and igor mentioned, i’m not entirely sure how you’d determine which images require renaming, unless you’re already enforcing a naming structure to the files.

In your example, you list 5 files, one of which does not match the pattern of the other 4.

If you had an enforced universal pattern, it should be relatively simple to do a limited-for array walk (from X+1 until Length, where X is a regex-found value from the deleted image) and apply the rename function.

Loops are not the devil, despite what JSLint tries to tell the world. glob is a function to get the wildcard-matched directory info, but at some level it also is doing loops. Deleting/renaming the target file is an atomic operation; and then you have an atomic operation that you need to occur on a dynamic number of elements.