IBM®
Skip to main content
    Country/region select      Terms of use
 
 
   
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks  >  Lotus  >  Forums & community  >  Best Practice Makes Perfect

Best Practice Makes Perfect

A collaboration with Domino developers about how to do it and how to get it right in Domino

A while back I posted a class to provide a NotesStream attached to a temporary file. People responded with suggestions, and on thinking about it (and getting another question about it recently), it seems to me there's a need for a more general class for creating a temporary folder for your temporary files (so that you don't overwrite anything important by accident), and deleting it and the files when you're all done. If it has the ability to generate a unique filename at need, it's easy enough to attach a NotesStream to it if that's what you needed, plus it's useful for other things, such as detaching files from a document.

Accordingly, the attached LotusScript code, described below. The class will locate the Notes client's temporary folder (this requires a call to a C function but I'm handling multiple platforms -- if yours isn't there you can easily enough add it). The class creates a folder within the Notes temp folder and remembers to delete it when the object is deleted -- assuming that you have been good and either deleted all the files in the folder yourself, or told the class about them so it can delete them.

If would be possible to write a version of this that would use Dir$ to find any files left in the folder and delete them itself, but this would still fail if any of the files were still open. Also, Dir$ doesn't work so hot with some filename characters.

Please note: when constructing a filepath using the value of the Path method, use "/" as your directory delimiter. This works on all client operating systems, and I think all servers of recent vintage also.

Also note: as indicated above, it's up to you to close any open files before the TempFolderManager object goes out of scope, or they will prevent the folder from being cleaned up.

CLASS TempFolderManager

Properties

  • Path (String) returns the temporary folder path. This path is unique to this object; if you create a new TempFolderManager you get a new path. You can create your own filename path by appending a filename to this (following a directory separator character: "/" works for this on all OS).
Methods
  • New( )
    Creates a new temporary folder for you. When the object is deleted it will attempt to delete the folder, but if the folder is not empty that will fail -- that's sort of up to you.
  • Function NewFilename(Byval strSuffix$, Byval bManage As Boolean) As String
    Creates a new unique filename and returns its full path in your temp folder. The file is not created -- that's up to you -- just the name. If you specify a suffix, that suffix will be appended to the filename after a period. Don't include the period in the suffix yourself. If bManage is true, the file will be deleted automatically when this object is deleted.
  • Sub Manage(Byval strPath$)
    If you create your own filename in the temp folder and you want to make sure the file gets deleted when you're done, call Manage with the full filepath of the file. Take care -- if you call this method you are asking to delete the file whose path you pass in -- eventually.
  • Sub Unmanage(Byval strPath$)
    If the file is in the list of files to be deleted, remove it from that list. The file will not be deleted when you're done. The path must match exactly the path that appears in the list of managed files, or this won't work.
  • Function ClearFiles( ) As Boolean
    Delete all the managed files immediately. This returns True if all managed files were successfully deleted. This lets you reuse the same folder for more files without worrying about name conflicts -- for instance, if you were processing attachments from a set of NotesDocuments and you wanted to extract them all into a temp folder, but clean up after each document without having to create a new folder.
Download the code here:

Andre Guirard | 5 May 2008 05:30:00 AM ET | | Comments (0)

Search this blog 

Disclaimer 

    About IBM Privacy Contact