Visual Basic for Applications/File and Folder Utilities

Summary

 * This first set of utilities concentrates on the basic FileSystemObject set; that is, the set used to find whether or not a file or folder exists, what their sizes are, and whether or not they have a particular attribute.  A basic path parsing procedure is also provided.   All of these procedures need a reference set in the VBA editor to Microsoft Scripting Runtime
 * No universally useful code was found for testing for open files. Although many procedures exist, they all fail in some way, usually failing to identify open text or image files, or Office files that are marked as read-only.   The basis of the problem is that many such files in Windows do not lock when opened by a user, so procedures that attempt to detect the open state by trying for sole access, cannot do so.   Any reader with a universal solution is, as always, invited to comment.

VBA Notes
At times it is useful to know whether or not a file or folder has a particular attribute, for example, to avoid hidden or system files in listings. The procedure HasAttribute does this, taking a path to the file as parameter and a short-code to identify the attribute of interest. However, the attribute bundle is delivered with all of the attribute number values added together, so this type of test, like other enumerations that involve constants (eg; the message box types), makes use of the AND function to split the bundle.

 For example: (See procedure HasAttribute below.) Assume that the attribute bundle from GetAttr equals 37 and that we are testing for the "system" attribute only ("S") with vbSystem = 4. Now, for numbers, the AND operator performs a bitwise AND on each column, so gives:

01001012 = 3710 = vbArchive + vbSystem + vbReadOnly 00001002 = 410 = vbSystem _______ 00001002 = 410, interpreted by boolean variables as True since it is non-zero

That is to say, the "system" attribute is present in the attribute bundle. If the "system" attribute were not set, then the result would have been all zeros

It is important to note that the returned value tests only one attribute at a time; that is to say, although a file returns true for for read-only ("R"), it might also have other attributes that are not tested. If users would rather have all of the file or folder attributes returned in one string, some work might be done to concatenate the result codes.

An example of file path parsing is given in the ParsePath procedure. The example uses the Split function to place all of the backslash separated terms into an array, then recombines them to make the path. A similar method, split on the dot is used to make the file name and suffix.