SetFileAttributes Method

Applies to TestComplete 14.92, last modified on September 16, 2021

Description

Use the SetFileAttributes method to change the attributes of the file which is described by the aqFileInfoObj object. To learn the existing attributes of the file, read the Attributes property.

Declaration

aqFileInfoObj.SetFileAttributes(fAttr)

aqFileInfoObj An expression, variable or parameter that specifies a reference to an aqFileInfo object
fAttr [in]    Required    Integer    
Result Integer

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

fAttr

An integer number that specifies the file’s new attributes. This parameter can accept one or a combination of constants listed in the following table (these constants are defined in the aqFileSystem object). To combine multiple values, use the bitwise OR operator.

Constant Value Description
faReadOnly 1 A read-only file.
faHidden 2 A hidden file.
faSystem 4 A system file.
faArchive 32 An archive file.
faNormal 128 A file that does not have any other attributes set.
faTemporary 256 A file that is used for temporary storage.
faOffline 4096 A file whose data is physically moved to offline storage.
faNotContentIndexed 8192 A file that is not to be indexed by the content indexing service.

Result Value

If the file attributes have been changed successfully, the method returns 0. Otherwise, it returns a Windows error code. To get the error description, you can use the aqUtils.SysErrorMessage method. For a complete list of error codes, refer to the System Error Codes article in the MSDN library.

Remarks

The following atttributes cannot be assigned by aqFileInfo.SetFileAttributes method:

Constant Value Reason
faDirectory 16 A file cannot be transformed to a folder and vice versa.
faSparseFile 512 Files are marked as sparse or not sparse via special tools and functions of the operating system.
faReparsePoint 1024 Reparse points are associated with files and folders via special tools and functions of the operating system.
faCompressed 2048 File and folder compression is enabled and disabled via specific tools and functions of the operating system.
faEncrypted 16384 Files and folder encryption is enabled and disabled via specific tools and functions of the operating system.
faVirtual 65536 Virtual files are created via special functions of the operating system.

Example

The following code snippet makes a file read-only and hidden while keeping other attributes unchanged:

JavaScript, JScript

var finfo = aqFileSystem.GetFileInfo("C:\\MyFile.txt");
finfo.SetFileAttributes(finfo.Attributes | aqFileSystem.faReadOnly | aqFileSystem.faHidden);

Python

finfo = aqFileSystem.GetFileInfo("C:\\MyFile.txt")
finfo.SetFileAttributes(finfo.Attributes | aqFileSystem.faReadOnly | aqFileSystem.faHidden)

VBScript

Dim finfo
Set finfo = aqFileSystem.GetFileInfo("C:\\MyFile.txt")
finfo.SetFileAttributes finfo.Attributes Or aqFileSystem.faReadOnly Or aqFileSystem.faHidden

DelphiScript

var finfo;
begin
  finfo := aqFileSystem.GetFileInfo('C:\\MyFile.txt');
  finfo.SetFileAttributes(finfo.Attributes or aqFileSystem.faReadOnly or aqFileSystem.faHidden);
end;

C++Script, C#Script

var finfo = aqFileSystem["GetFileInfo"]("C:\\MyFile.txt");
finfo["SetFileAttributes"](finfo["Attributes"] | aqFileSystem["faReadOnly"] | aqFileSystem["faHidden"]);

See Also

Working With Files From Scripts
Attributes Property
SysErrorMessage Method

Highlight search results