Topic: File Save and Delete

I'm using a FILE field to save my files.  I set the properties as follows:
  COPY TO = Files and TYPE = LinkFile
-
This works fine and does what I want.  And I can open up the file and edit it if I need to. 
-
However, say I have a yearly license form that I want to save.  I would like to replace the old license with the new copy.
-
I click on the FILE disk Icon and get a dropdown to OPEN FILE, OPEN FOLDER WITH FILE AND CLEAR.
When I choose CLEAR, it asks me Do you really want to delete the file from the database?
-
I answer yes and it clears the file name from the FILE field.
Then I hit save and I'm done.
Next I come back to the FILE field and ADD a new FILE to this same record.
It adds the file and puts the copy in the FILES folder as it should.
-
However the old file is still in the FILES folder.
-
It looks to me like if I did this with a number of Files that I would have extra files in the FILES folder that are orphans and don't belong to any particular record.
-
Any thoughts will be appreciated.
Thanks, Frank

2 (edited by derek 2020-10-18 15:38:01)

Re: File Save and Delete

Hi Frank,
For me, CopyTo and LinkFile are the correct way to approach what you're doing but it also needs a bit of care.
As you've found, when you click 'clear', it only removes the 'linkfile' filename from the relevant table - it doesn't remove the actual file in whatever 'copyto' folder you've specified. 
I guess there are situations where you wouldn't want that to happen and need to maintain some sort of 'trail'.  However, if, as in your case, you do want to remove files that are no longer needed, you have to do it by script.
Please have a look at the attached as one way of doing it. 
By way of explanation:
1.  On Form1, I hide the 'delete' button if there is a row that has a linkedfile associated with it (to prevent 'orphaned' documents).  A row can only be deleted once all associated files have been removed (see below).
2.  On Form2, I hide, by default, the standard 'file open, file directory open, clear' icon because of its ambiguity relating to 'clear'.
3.  On Form2, if the dbfile filename is empty, I display the standard 'folder' icon so a file can be selected and hide my own 'file delete' button (as there is nothing to delete at this point).
4.  On Form2, if there is a dbfile filename present, I display my own 'file delete' button and hide the standard 'folder' icon - this is prevent a new file being selected before the existing file has been deleted.
5.  On Form2, if my 'file delete' button is clicked, it removes the 'linkfile' filename from the table, it physically removes the file from the specified 'copyto' folder and clears out the filename in form2.dbfile1.
I think that covers most of the bases that you mentioned (or possibly more so just cut and paste from the attachment any bits that you need).
Derek.

Post's attachments

Attachment icon filedelete from copyfile folder.zip 366.06 kb, 21 downloads since 2020-10-18 

Re: File Save and Delete

Derek,
Your examples are great and look like they will take care of the 'orphaned' files.
-
It'll take me a while to go through your code & see what piece(s) I can add to my code. Being a newbie means that the code you wrote, probably in minutes, will take me a good while to go through and understand.  But that's OK because I'm learning something new every time you folks respond to one of my requests.
-
Seeing that I can turn off the "buttons" on the dbfile field is a nice feature and gives me some ideas about what I might be able to do.
-
The more I think about it, I believe most of my users will want to save a document and just leave it there.  Stuff like a marriage license or a birth certificate will most likely never need to be modified or changed.  And a document like a business license might only be updated once per year.  So 10 new files in 10 years isn't too bad.
-
So it might not be worth the effort to try to get rid of deleted files that might not happen very often.  So I'll have to think about it some more & see if it's something I really need to have.
-
Thanks for the help, Frank