Updating the file path in the files table of Drupal

Probably one of you already know this problem:

You have a path to a physical file (like an image) in a node. You like to change know this physical path. Therfore you have to update your node in order to link to the correct path again. That's ok if you update one file and one node. But, what happens if you rename for instance the image directory where your image module stored all the pictures of you galleries? This situation will force you easily to relink several 100 of pictures again .

 

I found the REPLACE function of MySQL very useful to solve this problem. If you move the images from the directory .../files/images/ to .../files/image, you could modify the files table (where Drupal stores the file pathes of the linked files) with the following update statement:

UPDATE `files` SET `filepath`=REPLACE(`filepath`, ".../files/images", ".../files/image");