VBScript Attributes Property
last modified April 9, 2025
The Attributes
property in VBScript is part of the
FileSystemObject
object model. It represents the attributes of a
file or folder, such as read-only, hidden, or system. These attributes are
stored as numeric values that can be combined using bitwise operations.
Attributes
allows checking and modifying file/folder properties
programmatically. It's essential for file management scripts that need to handle
special files. This tutorial covers Attributes
with practical
examples to demonstrate its usage.
Attributes Property Overview
The Attributes
property uses predefined constants to represent
different file attributes. Each attribute corresponds to a specific bit in a
numeric value. Multiple attributes can be combined using the OR operator.
Common attribute constants include Normal (0)
, ReadOnly
(1)
, Hidden (2)
, and System (4)
. The property
can be both read and modified. Understanding these values is key to working with
file attributes in VBScript.
Checking File Attributes
This example demonstrates how to check a file's attributes. We'll examine a file
to see if it's read-only or hidden. The script uses the GetFile
method to access the file object.
Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.GetFile("C:\test.txt") If file.Attributes And 1 Then WScript.Echo "File is read-only" End If If file.Attributes And 2 Then WScript.Echo "File is hidden" End If Set file = Nothing Set fso = Nothing
The script checks if specific bits are set in the Attributes value. The AND operator performs bitwise comparison with attribute constants. If the result is non-zero, the attribute is present. This method allows checking individual attributes from combined values.
Setting File Attributes
This example shows how to modify file attributes. We'll make a file read-only and hidden. The script demonstrates both setting individual attributes and combining multiple attributes.
Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.GetFile("C:\test.txt") ' Set read-only and hidden attributes file.Attributes = file.Attributes Or 1 Or 2 ' Alternative way using constants Const ReadOnly = 1 Const Hidden = 2 file.Attributes = file.Attributes Or ReadOnly Or Hidden WScript.Echo "Attributes set successfully" Set file = Nothing Set fso = Nothing
The script uses the OR operator to combine attributes without affecting others. Constants make the code more readable and maintainable. The operation preserves existing attributes while adding new ones. This is safer than direct assignment.
Removing File Attributes
This example demonstrates removing specific attributes from a file. We'll remove the read-only attribute while preserving others. The AND NOT operation clears specific bits.
Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.GetFile("C:\test.txt") ' Remove read-only attribute file.Attributes = file.Attributes And Not 1 ' Alternative using constant Const ReadOnly = 1 file.Attributes = file.Attributes And Not ReadOnly WScript.Echo "Read-only attribute removed" Set file = Nothing Set fso = Nothing
The script uses AND NOT to clear the read-only bit. Other attributes remain unchanged. This approach is precise and avoids unintended modifications. Constants improve code clarity when working with attribute values.
Listing All Attributes
This example shows how to list all attributes of a file. We'll check each possible attribute and display its status. The script demonstrates comprehensive attribute checking.
Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.GetFile("C:\test.txt") Const Normal = 0 Const ReadOnly = 1 Const Hidden = 2 Const System = 4 Const Volume = 8 Const Directory = 16 Const Archive = 32 Const Alias = 64 Const Compressed = 128 WScript.Echo "File attributes for: " & file.Path WScript.Echo "ReadOnly: " & CBool(file.Attributes And ReadOnly) WScript.Echo "Hidden: " & CBool(file.Attributes And Hidden) WScript.Echo "System: " & CBool(file.Attributes And System) WScript.Echo "Archive: " & CBool(file.Attributes And Archive) Set file = Nothing Set fso = Nothing
The script defines all attribute constants for clarity. Each attribute is checked using AND and displayed with CBool for true/false output. This approach provides a complete view of all file attributes. It's useful for diagnostic scripts.
Combining Multiple Attributes
This example demonstrates working with multiple attributes simultaneously. We'll create a script that toggles between normal and special attribute sets. The script shows attribute management in real-world scenarios.
Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.GetFile("C:\test.txt") Const SpecialAttributes = 1 + 2 + 4 ' ReadOnly + Hidden + System If file.Attributes And SpecialAttributes Then ' Remove special attributes file.Attributes = file.Attributes And Not SpecialAttributes WScript.Echo "Special attributes removed" Else ' Add special attributes file.Attributes = file.Attributes Or SpecialAttributes WScript.Echo "Special attributes added" End If Set file = Nothing Set fso = Nothing
The script combines multiple attributes into a single constant. It checks if any special attributes are set using a combined mask. The toggle operation either adds or removes all special attributes at once. This approach is efficient for batch attribute modifications.
Source
FileSystemObject Documentation
In this article, we have explored the Attributes
property in
VBScript, covering its usage and practical applications. From checking
individual attributes to complex combinations, these examples demonstrate
powerful file property management. With this knowledge, you can create robust
file handling scripts with precise attribute control.
Author
List all VBScript tutorials.