Purge IFS Files with Directory Pruning

Article ID: 57450

Q:I've downloaded your DELTREER4 tool ("Purge IFS Files Without QShell," August 10, 2006, article ID 52993). It works well and is definitely faster than QShell! But it doesn't delete empty directories. If you have a directory structure with a lot of levels, even after deleting all the files based on date, it leaves the directories behind. How can I delete directories as well?

A: In this article, I add a new option to the DELTREER4 program. The option checks whether the program has deleted the last file in a directory, and if so, it deletes the directory as well.

The rmdir() API is provided with the operating system and will delete an IFS directory, but only if that directory has no objects in it. If any objects are in the directory, the API returns an error CPE3488, "Directory not empty." With that in mind, the easiest way to tackle the problem is to change DELTREER4 to call rmdir() every time it deletes a file. If the file happens to be the last one in the directory, it deletes the directory as well.

To accomplish this task, I've made the following changes to DELTREER4:

  • I added a routine, dirname(), which figures out the name of the directory that a particular file is located in.

  • I added a PRUNE parameter to the DELTREE command. When set to *YES, it instructs DELTREER4 to delete directories. When set to *NO, it retains its old behavior of leaving directories alone.

  • Each time the DELTREER4 program deletes a file, it checks whether it's supposed to prune directories. If it is, it calls dirname() to get the directory name, and then calls rmdir() to delete the directory.

  • It ignores any CPE3488 errors. Any other errors will be written as *DIAG messages to the job log.

You can download the updated DELTREE utility right here.

ProVIP Sponsors

ProVIP Sponsors