The File object is available for client-side use with the MSIE browser. This property contains its file system attributes.
This property manages the attributes as a bit-mask with individual bits controlling different attributes. The bits can be accessed individually using the integer value corresponding to a power of 2. The table lists the integers that represent each different attribute:
Value | Attribute |
---|---|
0 | No special attributes - normal file. |
1 | Read only access. |
2 | Hidden file. |
4 | Indicates a system file. |
8 | Refers to drive volume label and cannot be altered. |
16 | Refers to a folder and cannot be changed. |
32 | File has changed and needs to be backed up again. |
64 | File object represents a shortcut and not a real file. |
128 | File is compressed. |
You can use Bitwise OR expressions to merge them or accomplish the same with integer additions.
You cannot alter the settings of bits 8, 16, 64 and 128 as these affect the structure of a file. That is to say, you cannot change a file into a folder or disk volume.
You should read the current attributes setting and then modify it to write it back. The example illustrates some simple functions that encapsulate this conveniently.
Where the bit needs to be set, a simple bitwise OR with a single bit value is accomplished in a single line. To clear a bit, we could use a bitwise AND having the corresponding bit clear. In these examples a different technique is used for illustration where the bit is set regardless of its previous state and is then cleared using a subtraction. That saves the computation of a complex bit-mask. An intermediate temporary variable is used to avoid signalling the operating system unnecessarily with modification requests.
There are other alternative ways to accomplish this and you could write some generic functions to examine, set or clear a bit in a bit-mask and then call them from each of these wrappers indicating the bit you want to operate on.
// Examine the read/write flag function isReadOnly(aFile) { return Boolean(aFile.Attributes & 1); } // Set the file read only function setReadOnly(aFile) { aFile.Attributes |= 1; } // Set the file read/write function setWriteOnly(aFile) { var myAttributes = aFile.Attributes |= 1; aFile.Attributes = myAttributes - 1; } // ---------------------------------------------------- // Examine the hidden flag function isHidden(aFile) { return Boolean(aFile.Attributes & 2); } // Hide the file function setHidden(aFile) { aFile.Attributes |= 2; } // Reveal the file function setVisible(aFile) { var myAttributes = aFile.Attributes |= 2; aFile.Attributes = myAttributes - 2; } // ---------------------------------------------------- // Examine the system flag function isSystemFile(aFile) { return Boolean(aFile.Attributes & 4); } // Set file to be a system file function setSystem(aFile) { aFile.Attributes |= 4; } // Set file to be a non system file function setPublic(aFile) { var myAttributes = aFile.Attributes |= 4; aFile.Attributes = myAttributes - 4; } // ---------------------------------------------------- // Examine the drive volume flag function isDriveVolume(aFile) { return Boolean(aFile.Attributes & 8); } // ---------------------------------------------------- // Examine the folder flag function isFolder(aFile) { return Boolean(aFile.Attributes & 16); } // ---------------------------------------------------- // Examine the backup flag function needsBackup(aFile) { return Boolean(aFile.Attributes & 32); } // Set backup required function setBackup(aFile) { aFile.Attributes |= 32; } // Clear backup function clearBackup(aFile) { var myAttributes = aFile.Attributes |= 32; aFile.Attributes = myAttributes - 32; } // ---------------------------------------------------- // Examine the shortcut flag function isShortCut(aFile) { return Boolean(aFile.Attributes & 64); } // ---------------------------------------------------- // Examine the compressed flag function isCompressed(aFile) { return Boolean(aFile.Attributes & 128); }
See also: | Folder.Attributes |
Prev | Home | Next |
File object | Up | File.Copy() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |