the REXX Files- by Dr. Dirk Terrell

This month I thought we would look at a utility written some time ago by Jeff Elkins called DAB. DAB stands for "delete all but" and is a REXX script that I use all the time. It is particularly useful when examining ZIP archives. After unzipping the archive and doing something, I want to delete all of the files except for the archive and DAB is just the tool to use. DAB is useful and illustrates some uses of the REXXUTIL library that comes with OS/2 REXX.

Since DAB uses the REXXUTIL library, it is necessary to ensure that the library has been loaded before we start:

CALL RxFuncAdd 'SysFileTree', 'RexxUtil', 'SysFileTree'
CALL RxFuncAdd 'SysFileDelete', 'RexxUtil', 'SysFileDelete'
Next we need to parse the arguments from the user. The calling form will be:

dab argument1 argument2 ... argument10

where argument1, argument2, etc. represent file specifications for the files that the user does not want to be deleted. The file specifications can have wildcards like * and ? just as you use from the OS/2 command line. For example, if the user wanted to delete all files except for ZIP files, the command would be:

dab *.zip

The program, as written, will accept up to ten arguments on the command line. I have never found this to be a limitation, but it could easily be rewritten to handle an arbitrary number of arguments. The code from DAB.CMD is:

ARG ext1 ext2 ext3 ext4 ext5 ext6 ext7 ext8 ext9 ext10
Once the arguments have been processed, the next thing to do is hide the files that match the arguments. This feat is accomplished with the SysFileTree function from the REXXUTIL library. The calling form for this function is:

SysFileTree(filespec,stem,options,target_attribute,new_attribute)

where filespec is file specification such as *.zip, stem is a stem variable to hold the results, and options is a string that specifies various options for the search. In options, F searches for files only, D searches for directories only, B searches for both files and directories, S recursively searches subdirectories, T returns dates in the form YY/MM/DD/HH/MM, and O returns only file names as opposed to dates, times, sizes, attributes and file names. FO for example would return only the names of the files in the current directory, while DO would return only the directory names.

The target_attribute and new_attribute options are masks that are used to match or set the file system attributes of the files. These attributes are:

The masks have a form like **+*- where + means the attribute is set, - means the attribute is not set, and * means either value. The order of the attributes in the mask is as given in the above list, ADHRS. So **+** for the target_attribute would only match files that had the hidden attribute set, and would set the hidden attribute of the matched files if it were specified as a new_attribute.

What we want to do is search for the files that the user has specified and set their hidden attribute so that we can delete everything else. The user has specified filespec, so all we need to do is call SysFileTree with options set to "F" (files only), target_attribute set to ***** (find all files) and new_attibute set to **-*** (set the matching files' hidden attribute). At this point all the files that the user specified are hidden.

Now we need to get a list of all the files that are left. Again we use SysFileTree, except this time we set filespec to *.*, target_attribute to **-** (only files that do not have the hidden attribute set), and new_attribute to ***** (don't change anything). With this list we can delete all of the files using the SysFileDelete function. The relevant code from DAB.CMD is:

rc=SysFileTree('*.*', del_file, 'FO', '**-**','*****')

df = del_file.0

DO x = 1 TO df
        rc = SysFileDelete(del_file.x)
        SAY del_file.x '........' file_error.RC
END
The last step is to use SysFileTree to make the hidden files visible again. This we do with target_attribute set to **+** and new_attribute set to **-**.

I made a modification to Jeff's code a while back that makes sure that at least one file in the current directory matches what the user has specified on the command line. I did this to prevent a typo from deleting all of the files in the directory. For example if the user mistakenly enters *.xip instead of *.zip, and there are no files that match *.xip, then everything in the directory would be deleted. The code to do this is commented in DAB.CMD, and you shouldn't have any problem following it. A nice modification might be to add a command line option that would list all of the files to be deleted and ask the user whether or not to proceed.

I have included my modifcations to the original DAB.CMD here for you to download. Enjoy!


Dr. Dirk Terrell is an astronomer at the University of Florida specializing in interacting binary stars. His hobbies include cave diving, martial arts, painting and writing OS/2 software such as HTML Wizard.

[Index]  [® Previous] - [Feedback] - [Next ¯]

[Our Sponsor: Keller Group Inc. - Developers of FaxWorks for OS/2 and PMfax.]


This page is maintained by Falcon Networking. We welcome your suggestions.

Copyright © 1997 - Falcon Networking