October 22, 2000 - Setting and Getting the File Attributes | WebReference

October 22, 2000 - Setting and Getting the File Attributes

Yehuda Shiran October 22, 2000
Setting and Getting the File Attributes
Tips: October 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

the File IO is based on the ActiveX technology. You create an ActiveX File object by calling ActiveXObject() with a single argument, Scripting.FileSystemObject:

myActiveXObject = new ActiveXObject("Scripting.FileSystemObject");

You can create a File object by using the ActiveX's GetFile() method:

file = myActiveXObject.GetFile("c:\\Autoexec.bat");

The File object supports various properties and methods, including the attributes property. The attributes property is an 8-bit bit property: every bit denotes a different file characteristic. To set or get a bit, you need to know how to compute powers of 2, up to and including 128. You set an attribute by setting its proper bit. To set the read-only characteristic, for example, you need to set the second bit:

file.attributes[1] = 1;

You can set the bit by a either a unit value or by a true value. To get a bit value, you need to use the Bit-And operation. The Bit-And operation returns true when the corresponding bit is set, and false otherwise. For example:

file.attributes & 32

returns true if the sixth bit is set, false otherwise. Here are all the attributes and their bit value. The Description column indicates the attribute values when the corresponding bit is set:

Bit ValueDescription
0A Normal file. No attributes set.
1File is read only. Read/Write.
2File is hidden. Read/Write.
4File is a system file. Read/Write.
8Disk drive volume label. Read only.
16Folder in a directory. Read only.
32File has changed since last backup. Read only.
64A link or shortcat. Read only.
128Disk drive volume label.

Learn more about ActiveX controls in Column 55, OLE Automation in JavaScript.