VB6Parse / Library / Filesystem / setattr

VB6 Library Reference

SetAttr Statement

Sets attribute information for a file.

Syntax

SetAttr pathname, attributes

Parts

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

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

Important Notes

Best Practices

See Also

References

← Back to Filesystem | View all statements