File AttributesFile attributes are aspects such as read-only, hidden, system, and archive that are used to configure how a file can be used by the operating system. These are the same attributes you can set via the attrib.exe command or the Properties Action menu in Explorer.exe, as seen in Figure 6-2. These attributes are not hidden from ordinary users (they are easily read in Explorer.exe), and they are used to control how backups run and to prevent accidental overwriting of important configuration and system files. This fact makes file attributes of interest to network administrators. Figure 6-2. File attributes can be read or set via the Attributes property
A file attribute is stored as a bitmask value to conserve space. When you query the file attribute, only a single integer is returned. When a file is hidden, VBScript returns a 2. When a file is a system file, VBScript returns a 4. When, however, a file is both a hidden file and a system file, VBScript return a 6 because the numbers get added together. The numbers are arranged so that each attribute or combination of attributes returns a unique numeric value. There are a number of possible combinations, each of which would need to be tested in a script returning these attributes. The bits representing each attribute value are listed in Table 6-3. A function that inteprets these combinations of integers is FunAttrib.vbs in the Utilities folder on the CD. This function is used in the FileAttributes.vbs script and will be examined shortly.
Just the Steps
Implementing the Attributes PropertyIn the FileAttributes.vbs script, you first use CreateObject to create an instance of FileSystemObject. Once the instance is created, you use GetFile to provide a reference to a specific file (in this case, C:\fso\test.txt). After you have a reference to the Test.txt file, you echo out the file name and also the attribute number by using the Attributes property in conjunction with the WScript.Echo command. Finally, you use a function that AND's the different values to build up a string that represents the exact file attributes that are set. When you AND two binary numbers, one AND one is equal to one. One AND zero is equal to zero. Zero AND zero is equal to zero. This is a great way to work with bitmask values. By using AND we can see if a value is present in a particular location in a bitmask number. This is seen in Figure 6-3. Figure 6-3. Use AND to identify file attributes
FileAttributes.vbs Option Explicit On Error Resume Next Dim objFSO Dim objFile Dim Target Target = "C:\fso\test.txt" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.GetFile(Target) WScript.Echo "The file is: " & target WScript.Echo "bitmap number is: " & objFile.Attributes & _ " " & funAttrib(objFile.attributes) Function funAttrib(intMask) Dim strAttrib If IntMask = 0 Then strAttrib = "No attributes" If intMask And 1 Then strAttrib = strAttrib & "Read Only, " If intMask And 2 Then strAttrib = strAttrib & "Hidden, " If intMask And 4 Then strAttrib = strAttrib & "System, " If intMask And 8 Then strAttrib = strAttrib & "Volume, " If intMask And 16 Then strAttrib = strAttrib & "Directory, " If intMask And 32 Then strAttrib = strAttrib & "Archive, " If intMask And 64 Then strAttrib = strAttrib & "Alias, " If intMask And 2048 Then strAttrib = strAttrib & "Compressed, " funAttrib = strAttrib End Function More Info Setting File AttributesYou have to assign a numeric value to the Attribute property to set the file attributes for a file. This can be as simple as setting the Attribute property of the file to be equal to an integer value, as seen below: objFile.Attributes = intAttrib In the SetFileAttributes.vbs script, we first create an instance of the file system object and set it equal to the variable objFSO, as we have done in other scripts. We next return a file object by using the GetFile method from the file system object. Once we have a file object, we query the Attributes property, use the funAttrib function to parse the value, and print it out. Next we enter the subSetAttrib subroutine, where we assign the desired value for the attribute to the file. Once we have done that, we once again use the funAttrib function to translate the new value. SetFileAttributes.vbs Option Explicit On Error Resume Next Dim objFSO 'the file system object Dim objFile 'the file object Dim strTarget 'path to target file Dim intAttrib 'desired file attribute combination strTarget = "C:\fso\test.txt" intAttrib = 0 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.GetFile(strTarget) WScript.Echo "The file is: " & strTarget WScript.Echo "OLD bitmap number is: " & objFile.Attributes & _ " " & funAttrib(objFile.attributes) & vbNewLine subSetAttrib ' *** subs and functions below ***** Function funAttrib(intMask) Dim strAttrib If IntMask = 0 Then strAttrib = "No attributes" If intMask And 1 Then strAttrib = strAttrib & "Read Only, " If intMask And 2 Then strAttrib = strAttrib & "Hidden, " If intMask And 4 Then strAttrib = strAttrib & "System, " If intMask And 8 Then strAttrib = strAttrib & "Volume, " If intMask And 16 Then strAttrib = strAttrib & "Directory, " If intMask And 32 Then strAttrib = strAttrib & "Archive, " If intMask And 64 Then strAttrib = strAttrib & "Alias, " If intMask And 2048 Then strAttrib = strAttrib & "Compressed, " funAttrib = strAttrib End Function Sub subSetAttrib objFile.Attributes = intAttrib WScript.Echo "The new attibutes are: " & funAttrib(objFile.Attributes) End Sub |