SetAttr Statement
Sets attribute information for a file.
Syntax
SetAttr pathname, attributes
Parts
- pathname: Required. String expression that specifies a file name. May include directory or folder, and drive.
- attributes: Required. Numeric expression or constant specifying the file attributes. Sum of the values of the file attribute constants.
File Attribute Constants
| Constant | Value | Description |
|---|---|---|
| vbNormal | 0 | Normal (no attributes set) |
| vbReadOnly | 1 | Read-only file attribute |
| vbHidden | 2 | Hidden file attribute |
| vbSystem | 4 | System file attribute |
| vbArchive | 32 | File has changed since last backup |
Remarks
- Combining Attributes: You can combine attributes by adding their values together (e.g.,
vbReadOnly + vbHidden = 3). - File Must Exist: A run-time error occurs if the file specified by pathname doesn't exist.
- Pathname Validation: Pathname can be a fully qualified path or a relative path. Wildcard characters (* and ?) are not supported.
- Cannot Set Directory Attribute: You cannot use
SetAttrto set the directory (vbDirectory = 16) attribute. UseMkDirandRmDirinstead. - Volume Label: You cannot use
SetAttrto set the volume label (vbVolume = 8) attribute. - Read-Only Directories:
SetAttrcannot change the read-only status of a directory; it only works with files. - Error Handling: Use error handling to trap potential errors like file not found, permission denied, or invalid attributes.
GetAttrFunction: UseGetAttrto retrieve current file attributes before modifying them withSetAttr.- Clearing Attributes: To clear an attribute, set the file to vbNormal (0) or use a combination that excludes the unwanted attribute.
Examples
Set File to Read-Only
SetAttr "C:\MyFile.txt", vbReadOnly
Set File to Hidden
SetAttr "C:\Data\Secret.dat", vbHidden
Combine Multiple Attributes
' Set file to read-only and hidden
SetAttr "C:\Config.ini", vbReadOnly + vbHidden
Clear All Attributes (Normal)
SetAttr "C:\MyFile.txt", vbNormal
Set Archive Attribute
SetAttr "C:\Backup\Data.dat", vbArchive
Using Variables
Dim fileName As String
Dim attrs As Integer
fileName = "C:\Data\MyFile.txt"
attrs = vbReadOnly + vbArchive
SetAttr fileName, attrs
Toggle Read-Only Attribute
Dim currentAttrs As Integer
Dim filePath As String
filePath = "C:\MyFile.txt"
currentAttrs = GetAttr(filePath)
If currentAttrs And vbReadOnly Then
' Remove read-only
SetAttr filePath, currentAttrs And Not vbReadOnly
Else
' Add read-only
SetAttr filePath, currentAttrs Or vbReadOnly
End If
Set System File
SetAttr "C:\Windows\system.dat", vbSystem
Set Multiple Files in a Loop
Dim i As Integer
For i = 1 To 10
SetAttr "C:\Files\File" & i & ".txt", vbReadOnly
Next i
With Error Handling
On Error Resume Next
SetAttr "C:\MyFile.txt", vbReadOnly
If Err.Number <> 0 Then
MsgBox "Could not set file attributes: " & Err.Description
End If
On Error GoTo 0
Using App.Path
SetAttr App.Path & "\Config.ini", vbHidden
Preserve Existing Attributes While Adding New Ones
Dim filePath As String
Dim currentAttrs As Integer
filePath = "C:\MyFile.txt"
currentAttrs = GetAttr(filePath)
' Add hidden attribute while preserving others
SetAttr filePath, currentAttrs Or vbHidden
Remove Specific Attribute
Dim filePath As String
Dim currentAttrs As Integer
filePath = "C:\MyFile.txt"
currentAttrs = GetAttr(filePath)
' Remove hidden attribute while preserving others
SetAttr filePath, currentAttrs And Not vbHidden
Using Numeric Values
SetAttr "C:\MyFile.txt", 1 ' Same as vbReadOnly
SetAttr "C:\MyFile.txt", 3 ' Read-only + Hidden (1 + 2)
SetAttr "C:\MyFile.txt", 35 ' Read-only + Hidden + Archive (1 + 2 + 32)
Conditional Attribute Setting
If FileIsImportant Then
SetAttr filePath, vbReadOnly + vbArchive
Else
SetAttr filePath, vbNormal
End If
Common Errors
- Error 53: File not found - occurs if the pathname doesn't exist
- Error 5: Invalid procedure call or argument - occurs if attributes value is invalid
- Error 70: Permission denied - occurs if you don't have write access to the file
- Error 75: Path/File access error - occurs if the file is open or locked
Important Notes
- File Must Be Closed: The file should not be open when you use
SetAttr. - Permissions Required: You must have appropriate permissions to change file attributes.
- Network Files:
SetAttrworks with network files if you have appropriate permissions. - UNC Paths:
SetAttrsupports UNC (Universal Naming Convention) paths like "\\Server\Share\File.txt". - Attribute Persistence: File attributes persist after the application closes; they're stored in the file system.
- Read-Only Files: To modify a read-only file, you must first remove the read-only attribute, make changes, then restore it.
GetAttrComplement: Always useGetAttrto retrieve current attributes before modifying them to avoid unintentionally removing existing attributes.
Best Practices
- Use error handling when working with
SetAttras file operations can fail for many reasons - Use
GetAttrbeforeSetAttrto preserve existing attributes you don't want to change - Use symbolic constants (vbReadOnly, etc.) instead of numeric values for better code readability
- Check file existence using
Dir()before callingSetAttr - Be cautious when setting system attributes as they can affect system stability
- Document why specific attributes are being set, especially for hidden or system files
- Consider user permissions when setting attributes on shared or network files
See Also
GetAttrfunction (retrieve file attributes)Dirfunction (check if file exists)Killstatement (delete files)Namestatement (rename files)FileCopystatement (copy files)