1. Tracy Lamb
  2. PowerBuilder
  3. Wednesday, 19 January 2022 15:39 PM UTC

Hi folks,

My app creates a temporary directory then adds an unspecified number of pdf reports to it.  When I'm done, I want to delete the whole directory.  RemoveDirectory() requires the directory to be empty. 

FileDelete() requires the file name.  Can I use a wild card in FileDelete() to delete all pdf files?

DirList() populates a list box.  I could add an invisible list box to the window, then loop through and delete each file.  I'm wonder if this is necessary, or if there's a more direct way to do this?

~~~Tracy

 

 

 

Accepted Answer
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Wednesday, 19 January 2022 16:14 PM UTC
  2. PowerBuilder
  3. # Permalink

Hi Tracy;

  FWIW: For these type of things, I just have my PB App call the DOS command RMDIR /S for delete all files and the temp folder. If you want only the files to be removed but not the folder, then I would use the DEL *.* DOS command using the Run() command.  HTH

Regards ... Chris

Comment
  1. Tracy Lamb
  2. Wednesday, 19 January 2022 17:50 PM UTC
So I'm testing the command line option right now...

In the command window, I type: RMDIR /Q /S "C:\BenchTop10\_New Customer10\PDFMerge"

This works fine

In my code, I build the RUN command:

ls_run = 'RMDIR /Q /S "' + is_currentDir + 'PDFmerge"'

MessageBox("run", ls_run) --- ls_run is exactly the same as what I manually typed into the command line ---



li_rc = RUN(ls_run)

if li_rc = 1 then

this.enabled = FALSE

end if

MessageBox("rc", li_rc)

li_rc is always -1 and the directory is not deleted...



Any ideas?

  1. Helpful
  1. Brad Mettee
  2. Wednesday, 19 January 2022 18:06 PM UTC
It sounds like your current working directory might still be PDFMerge when you try to delete it. Make sure you change directory in PBScript to someplace else prior to running your batch file.
  1. Helpful
  1. Tracy Lamb
  2. Wednesday, 19 January 2022 18:31 PM UTC
I had to change the RUN command string a bit to add "CMD /C"... probably a rookie move!

ls_run = 'CMD /C RMDIR /Q /S "' + is_currentDir + 'PDFmerge"'

  1. Helpful 1
There are no comments made yet.
Mark Goldsmith Accepted Answer Pending Moderation
  1. Wednesday, 19 January 2022 18:12 PM UTC
  2. PowerBuilder
  3. # 1

Hi Tracy,

To your question of wildcards in FileDelete(), no the file must be specifically named. I do like what Brad has suggested in his first reply as it gives you some options and flexibility in the event that's important to you and I have used it on occasion. If that's not important to you and you are confident and comfortable deleting an entire directory and all it's contents then, as Chris suggested, issue a DOS command with the Run() function. More specifically, the syntax would look something like this:

Run('CMD /C "RMDIR /S /Q D:\MAIN_FOLDER\sub_folder"', Minimized!)

The /C is to ensure the CMD window terminates. The /S is to include all subfolders and files and the /Q is to prevent the "Are you sure..."
prompt.

Respectfully, I'm less concerned about the safety issue that Brad raises about deleting the wrong directory as well as the concern about interacting with the DOS command for the following reasons:


1) You're already keeping track of the temporary directory and so that would be explicitly stated in the Run() function; should the temporary directory not always be the same or the same full path (EG different users), or you want the flexibility to change it, such that you can't hardcode it in the Run() function then simply build the string for the Run() function using a string variable...so unless I'm misunderstanding Brad's concern, I can't see how you would accidentally delete the wrong directory/ files. My approach has always been to rely on PBScript first in order to accomplish something but I wouldn't hesitate to utilize shelling out to DOS or using Windows API calls if the need/ preference arises.

2) Per the /Q option above (which also works with Del *.*), this will prevent the DOS confirmation prompt and so eliminates any user interaction

As Mike and Miguel have suggested, there is the potential for this to go sideways should there be a crash of the application but I think you can plan for this by ensuring that when you first create the temporary folder it doesn't already exist and if it does then delete it using the same approach as above. Of course, that depends on how you are naming the temporary directory...is it always the same name but maybe a different path per user? Is it always a different name that you're randomly generating but in the same path? Are you coding for multiple users for this functionality? The danger with randomly generated directory names is, over time with multiple crashes, you'll accumulate many orphaned directories/ files that will never be deleted unless you manually look after these after each crash.

HTH...regards,

Mark

Comment
  1. Mark Goldsmith
  2. Wednesday, 19 January 2022 18:48 PM UTC
You're welcome Tracy and glad to see that syntax worked for you.
  1. Helpful
  1. Chris Pollach @Appeon
  2. Wednesday, 19 January 2022 18:59 PM UTC
Awesome news Tracy!
  1. Helpful
  1. Brad Mettee
  2. Thursday, 20 January 2022 07:22 AM UTC
Just out of curiosity, what happens if this app gets moved to a server (or remotely accessed system) and multiple people are running the report at the same time? Will files get deleted before being used? (or during?)
  1. Helpful
There are no comments made yet.
Ronnie Po Accepted Answer Pending Moderation
  1. Wednesday, 19 January 2022 17:48 PM UTC
  2. PowerBuilder
  3. # 2

If it's just a scratch directory, I'd use Roland's RunAndWait to run the DOS command

rmdir <directory> /s /q

To clean up after a crash, you can also run the command when your app starts up.

Comment
There are no comments made yet.
mike S Accepted Answer Pending Moderation
  1. Wednesday, 19 January 2022 17:13 PM UTC
  2. PowerBuilder
  3. # 3

*** this was supposed to be a response to Brad's suggestion of keeping a list and delete just the list.  

 

the downside to this approach (of keeping your own list) is that if your application crashes then those files that were created in that session won't be deleted. I use this exact approach of delete only the list, and i see left over files from crashes.

maybe a combination of both approaches if speed really is a concern.

there are windows apis to get the files in a directory. its stupid that we have to drop down to api's for something like this rather than have a PB native command.

Comment
  1. Miguel Leeuwe
  2. Wednesday, 19 January 2022 17:21 PM UTC
Yes, but the same thing would happen with a crash and deleting files using a batch file. (Unless your batch file deletes every file in the directory). What I usually do,first delete all the 'old' temp files before writing any new files. I there was a crash, some next execution will clear them out.



I made a small sample app not too long ago to list all PBL files on a disk using Roland Smith's stuff in a recursive manner. In case it might be useful for anyone: https://drive.google.com/uc?export=download&id=1-N0vt5TZXE723TWKK73BaDl_21Pajt4c
  1. Helpful
  1. Miguel Leeuwe
  2. Wednesday, 19 January 2022 17:23 PM UTC
  1. Helpful
There are no comments made yet.
Brad Mettee Accepted Answer Pending Moderation
  1. Wednesday, 19 January 2022 15:59 PM UTC
  2. PowerBuilder
  3. # 4

Tracy,

I'd suggest keeping an internal list of files added to the temporary directory, and deleting based on that list.

Two reasons for this:

1. If you ever end up using a shared directory, other people might have files in there that you don't want to delete early.
2. It's a lot quicker if you don't have to go find out what files exist in the directory. It also means you can determine what order to delete them in (if that's an issue), and whether or not you want to selectively NOT delete some of them (debugging).

Along with number 1, making sure you have unique temp filenames should be taken into consideration as well.

 

Comment
  1. Miguel Leeuwe
  2. Wednesday, 19 January 2022 16:45 PM UTC
Totally agree: If the pdf files are being written by your application, it should not be to hard to put their names in a string array or even on a datastore. Once you are finished, you just loop through that array or datastore and delete each file using FileDelete().
  1. Helpful
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.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.