Set Attrpathname attributes

The SetAttrQ function sets the attributes of a file. The argument pathname is the path name of an existing file and attributes is a numeric value that specifies one or more attributes. To set an attribute without affecting any existing attributes, use a statement like

SetAttr(pathname, GetAttr(fi1e_name) Or new_attribute)

The new_attribute argument can have any of the values shown in Table 5 in the preceding section.

To change multiple attributes, combine the corresponding values with the logical OR operator. Notice that the statement

SetAttr(fi1e_name, new_attribute)

will turn on a specific attribute, but it will clear all other attributes. If a file is read-only and hidden, its Attributes property is 3 (1 + 2 according to Table 5). If you attempt to turn on the Archive attribute by setting its Attributes property to 32, the other two attributes will be cleared. By combining the new attribute (32) and the existing attributes with the OR operator, the file will be readonly, hidden, and archive.

To remove a specific attribute, first find out whether this attribute is already set, and then subtract its value from the value returned by the GetAttr() function. To remove the Hidden attribute, use a structure such as the following:

If GetAttr(fi1e_name) And FileAttribute.Hidden Then

SetAttr(fi1e_name, GetAttr(fi1e_name) - FileAttribute.Hidden) End If

You can also use the MsgBox() function to prompt the user to change the read-only attribute:

If GetAttr(fi1e_name) And FileAttribute.ReadOnly Then reply = MsgBox("This is a read-only file. Delete it anyway?", _

MsgBoxStyle.YesNo) If reply = MsgBoxResult.Yes Then

SetAttr(fi1e_name, GetAttr(fi1e_name) - FileAttribute.ReadOnly) Ki11(fi1e_name) End If Else

Ki11(fi1e_name) End If

You can also use the XOR operator to reset an attribute. The call to the SetAttr() function can also be written as follows (the two methods of resetting an attribute are equivalent):

SetAttr(fi1e_name, GetAttr(fi1e_name) Xor FileAttribute.ReadOnly)

0 0

Post a comment