Re: File Removal after Database Comparison



Chris Owens wrote:
Hi all,

I'm trying to figure the logic (and therefor the code) to enable the clean-up of a site I've inherited. Basically I have a folder that has around 6,666 images in it that are loosely related to the information held in a database. I say loosely because the images are uploaded (via FTP) manually. There is no direct reference to the JPEGs in the database, as the code on the site references them via:

<img src="<?=echo ($referenceid); ?>_f.jpg" />
<img src="<?=echo ($referenceid); ?>_s.jpg" />
<img src="<?=echo ($referenceid); ?>_b.jpg" />

Each record in the database has three related images:

Image of the front - recordid_f.jpg
Image of the side - recordid_s.jpg
Image of the back - recordid_b.jpg

If the image file names really contain the database id like
2177_s.jpg
2177_b.jpg
.... etc
It would be a matter of (think unix but similar in windows shell)
ls -1 *_s.jpg > fnames.txt
to capture all file names.
After that, scratch some code code together to collect the numbers
from fnames.txt entry (easy in any language).

Create new folder nearby.

write script that does this:
open database
for each number in list
.. query database for the number
.. if in the db:
.. stringify the number to recreate filenames
.. copy files to new dir

When this is done, ONLY the images you want to keep will be
in the new folder. Once you are sure it worked, delete all originals.
This is the safe way.

Faster (but test carefully first):
open database
for each number in list
.. query database for the number
.. if NOT in the db:
.. stringify the number to recreate filenames
.. delete files


Now over time records have been removed from the database, but the images haven't been removed and as such, short of removing all images, there is no easy way that I can think of to clear the folder (several GB of data) whilst keeping the images that have related records in the db.

My question is: How can I through code remove any orphaned images (that aren't needed anymore), whilst keeping the images that still relate to records in the database?

Any assistance/pointers/snippets would be much appreciated.

Kind regards,

Chris

.



Relevant Pages

  • Re: which is the better option for directory hashing to store large number of image files?
    ... images for a given order. ... I create a folder under photos with ... would want to put all those as fields in a database table and have the full ... that way I can only store the name of the image. ...
    (comp.lang.php)
  • Re: [PHP] Re: how to display images stored in DB*
    ... Your claim is that in ALL cases using a file system to store images ... bitmaps in a database without a damn good reason, a bad practice, yes. ...
    (php.general)
  • Re: [PHP] Re: how to display images stored in DB**
    ... for storing images is "bad" practice. ... "Bad" practice or "good" practice depends upon the needs and one's perspective of the situation. ... If you were to talk to the average computer user; or to the average investor who invests in technologies; or to the businessman who is paying the bills for programmers to build the product while trying to make money at the same time; they all might have different opinions as to the quality and state of software. ... If you assume that you have one database supply all the images to all ...
    (php.general)
  • Re: bound object frame: picture
    ... Please let me respond to your critique and hopefully help Bob along the way. ... generates can be very large when they contain images. ... Print event NOT THE FORMAT EVENT. ... your entire database will stop working when you get about 100 pictures ...
    (microsoft.public.access.forms)
  • Re: ListView - Caching Thumbnails?
    ... The program just will yield some odd results if they do (which I figure they can deal with if they are monkeying with the database;). ... Access databases fill the middle ground I guess but still, you can edit XML by hand. ... The underlying goal here is to create an interactive anatomical atlas program using high resolution images. ... Then I go back to school ). ...
    (microsoft.public.dotnet.languages.vb)