I've discovered a number of legacy files amongst my image files have "+" in the filename which seems to be bothering some requests - i.e. the image does not display (although it used to display at one time in Backdrop on the pages.

Since I have hundreds of them is there a way to change the filename by replacing the "+" with "-and-" and not end up with duplicates in the Image Library?

And while I'm here - how do you remove unwanted images from the Image Library?

Accepted answer

OK, I put together a quick and dirty module for you:

https://github.com/argiepiano/fix_my_files

It will replace the "+" with "-and-" in the file name and uri. After installing, visit the path example.com/fix-my-files

This will NOT change the actual file names in the files folder! Only the database records. You'll need to rename the files manually using shell access and rename or any other method. 

IMPORTANT:

  1. Be sure to back up your database and your files before running 
  2. If you have many files, this module may time out before finishing Ideally, this renaming should be done in a batch process. For simplicity, it doesn't. So,  if you have more than, say, 500 files to rename, this may create problems. You may want to increase your PHP max_execution_time init value to a few minutes before running this.

Most helpful answers

Yes, it works directly with the file entities, and therefore it will update the relevant tables in the database. Backdrop is smart that way :) 

Comments

Are you comfortable creating a custom module? AFAIK there is no UI-based or contrib module capable of doing this in bulk.

The way I would approach this is by:

  1. Use a terminal/shell command like rename using regex to rename all your files directly in the public file folder
  2. Then create a module that:
    1. Loads all file entities ($files = file_load_multiple(FALSE);)
    2. Loops through them
    3. Uses preg_replace to replace the "+" in both, the filename property (which stores the label of the file entity, not the actual "file name" of the file on disk), AND the uri property, which points at the actual file on disk
    4. Saves the file entity ($file->save();)

 

Needless to say, BACK UP your site AND your files folder before attempting any of this!

Thanks for the reply and the advice Alejandro - sadly I am not comfortable creating a module. I'd have to do some serious research and learning before tackling that! :D

Basically this was just a side thought while trying to resolve some Rewrite Rule problems.

No problem. You should look into module creation - it's much simpler than what most people think, just a few lines. It comes handy when you want to do one-time things like this one, or when you want to customize core or cnotrib forms, for example.

I've started to read up on creating modules, but I think this is waaay out of my league at the moment due to what I perceive as the complexity of my issue.

  1. find all files with "+" in the filename.
  2. rename them
  3. find all instances of said filename in the DB tables
  4. change the DB tables to show new filename
  5. Find all nodes referencing the old filename and change to new filename.

Phew!

Still looking for a way to achieve image name change in Image Library or some other way that does not involve writing a module.

I thought your Jan 21 response meant you had solved this issue by editing things directly on the database?

No Alejandro - those were my thoughts on what would be necessary if a custom module had to be written. Its just me thinking about the steps needed in order to change things... I continue to ponder a solution.

I see. Writing a module to do this is VERY simple. I bet this can be accomplished with 10 lines of code.

OK, I put together a quick and dirty module for you:

https://github.com/argiepiano/fix_my_files

It will replace the "+" with "-and-" in the file name and uri. After installing, visit the path example.com/fix-my-files

This will NOT change the actual file names in the files folder! Only the database records. You'll need to rename the files manually using shell access and rename or any other method. 

IMPORTANT:

  1. Be sure to back up your database and your files before running 
  2. If you have many files, this module may time out before finishing Ideally, this renaming should be done in a batch process. For simplicity, it doesn't. So,  if you have more than, say, 500 files to rename, this may create problems. You may want to increase your PHP max_execution_time init value to a few minutes before running this.

That was very kind of you Alejandro! I have downloaded and will inspect the code after dinner (NZDT) for clues as to the process. Then I will try it out once I've identified all the files that are affected.

I cannot thank you enough for your help.

Ian

Alejandro - I've had time to look at the code and it seems simple.

I did a quick and dirty script to check for files with + in filename and it tells me there are 3300 spread throughout a number of sub-folders under /files such as coll (and its sub-folders), styles (and its sub-folders), and Moybyz.

I will install the module and look further into how it works.

Q. how does it know which database tables to update?

Looking at it, it works with the file entity so doesn't directly update the database but updates the file entity which in turn updates the relevant tables.

@yorkshirepudding  Thanks for the explanation - my code writing skills are so poor I could not see anything in the code that pointed me to that answer. 

Yes, it works directly with the file entities, and therefore it will update the relevant tables in the database. Backdrop is smart that way :) 

Alejandro - Thanks for the additional information. Backdrop is very smart, but I'm not! 

Hi Alejandro - how will I know when it is finished?

BTW - getting many errors in dblog:

Warning: filesize(): stat failed for public://toys/Matchbox/Lesney/Item03032/LesneyMilbro_BaitPress_Red-and-Green_Item03032-1.jpg in FileStorageController->preSave() (line 289 of /var/www/toysnz.com/htdocs/backdrop/core/modules/file/file.entity.inc).

It's finished once the browser reloads the form again.

Regarding the warning: it looks like it's thrown when Backdrop tries to load the file record - it's not finding the original file that contained the "+" sign, which throws warning when backdrop tries to get the filesize of such misssing file.

I believe that warning can be ignored, but I'd have to look more closely. To check if this worked, head to admin/content/files and click on any of the files that was renamed. Is the image shown? Then click on Manage. Does the info show the modified name and uri, as in?:

 

Thanks for checking that Alejandro. I checked the files - image does not show in files/manage, but the file title and file url have both been changed. I guess I should have renamed files before running the module.

Going by the dblog it seems to have only taken a minute to do all 3337 files.

Alejandro - I've successfully renamed my files using your module. I discovered some other files with hash in them so I modified it to go look for them and change them from "#" to "No".

Following the exercise I noted I was still getting the "Warning: filesize(): stat failed for public://...." error each time I uploaded an image file but disabling the module seems to have fixed that. (Just FYI).

I've marked your reply as "Accepted"... again many thanks for your help. I've learned a little more about Backdrop thanks to you.