1. Tracy Lamb
  2. PowerBuilder
  3. Sunday, 21 August 2022 14:31 PM UTC

Hi friends,

There are a few places in my App that let as user "attach" a PDF to a specific record.  The app creates a directory for the pdf if a matching directory doesn't already exist.  Then it copies the selected file and writes a record to the DB.  

The user can also delete an "attachment".  If the document the user deletes is the only document in the directory, I'd like to delete the directory too.  The only thing I can think of is to go ahead and try to remove the directory after each attachment is deleted, trusting that RemoveDirectory () will fail if there are still files in the directory.  

li_filenum = RemoveDirectory ( ls_path )
If li_filenum = 1 then
MessageBox("Remove directory succeeded", &
   + "This was the last file in directory: " &
   + ls_path +"~r~nDirectory was removed successfully.")
end if

I'm not sure I even need the MessageBox.  If the user adds another file after deleting the last one, the directory will automagically be created again.

Just wondering if there's a smarter way to do this.

~~~Tracy

 

Accepted Answer
Tracy Lamb Accepted Answer Pending Moderation
  1. Sunday, 21 August 2022 15:05 PM UTC
  2. PowerBuilder
  3. # Permalink

I just decided to use RemoveDirectory( ls_path).  Works good.  If there are files in the directory, RemoveDirectory() fails quietly.  If there aren't files in the directory, the directory is removed quietly.  When I add another file after deleting the last one, the directory is re-created.

~~~Tracy

 

 

Comment
  1. Tracy Lamb
  2. Sunday, 21 August 2022 18:46 PM UTC
I think I've got that covered... according to an earlier question I asked, GetFileOpenName() and GetFileSaveName() both change the current directory. I don't use GetFileSaveName() anywhere in my code, but do use GetFileOpenName() in the ue_add_doc event. If the user tries to add a document from the same directory, they'll get a message that the file already exists, and the event exits without doing anything.

In the App's open event, I get the current directory and save that in a global variable. In some cases, I need the app's directory to create temporary directories and files, and to update the app.ini file.

Long story short, the CurrentDirectory() would never be the same as the directory I want to delete, and if the RemoveDirectory() fails for that reason, it doesn't really matter.

  1. Helpful
  1. Mark Goldsmith
  2. Sunday, 21 August 2022 19:00 PM UTC
Yes, sounds like you have it covered ;-)
  1. Helpful
  1. Tracy Lamb
  2. Monday, 22 August 2022 02:41 AM UTC
Quick update: I decided to change the directory back to the app directory by calling ChangeDirectory(gs_AppDirectory) after I call GetFileOpenName(), just to be safe!
  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Sunday, 21 August 2022 14:37 PM UTC
  2. PowerBuilder
  3. # 1

Hi Tracy;

  Have you tried using the DirList() method for this?

https://docs.appeon.com/pb2022/powerscript_reference/dirList_func.html

Regards... Chris 

Comment
  1. Tracy Lamb
  2. Sunday, 21 August 2022 14:41 PM UTC
I thought about it, but that's a function of a list box, and I don't have a list box. I have a datawindow.
  1. Helpful
  1. Chris Pollach @Appeon
  2. Sunday, 21 August 2022 14:47 PM UTC
I would just add a hidden LB to your window where the DC/DS exists.
  1. Helpful
  1. Roland Smith
  2. Monday, 22 August 2022 02:51 AM UTC
I have file system functions in this example that you can use:

https://www.topwizprogramming.com/freecode_filesys.html

You can call of_GetFiles and see how many file names are returned.
  1. Helpful 1
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.