in reply to Re: Delete files if they match help!
in thread Delete files if they match help!

Well, in the database only the name of these files exist, in the directory in spec is where the physical files are, that’s why I need to check if they match first before deleting them, any more feedback? Thanks!

Replies are listed 'Best First'.
Re^3: Delete files if they match help!
by Anonyrnous Monk (Hermit) on Jan 07, 2011 at 14:57 UTC

    I'm afraid I still don't understand the need for the extra checking.  If you have a name, say "img_2678.jpg", in the DB that you want to delete from the directory ../../pics, you'd just create the proper path and do

    unlink "../../pics/img_2678.jpg";

    The OS will then try to locate the physical file and, if it's there, delete it.  If it's not there, the unlink call will fail. That's it. No need to first figure out if the directory holds a file img_2678.jpg.

    Or is it that you don't know which directory holds the files? In this case, just check the return value of the unlink call. If it succeeded, you likely picked the right one, otherwise try the next directory candidate.

    (The same general considerations would apply if you're actually going to move the files, instead of deleting them.)