Advanced Files and Folders in WSH - Changing File Attributes
(Page 4 of 4 )
We’ve seen how to check for the existence of a file attribute. Now let’s learn how to set them. We can simply assign the Attributes property with whatever attribute value we choose in order to change it.
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.GetFile("C:boot.ini")
objFile.Attributes = 35
This code sample essentially removes the System attribute from the boot.ini file. We’ve manually set it to 35 which is: Read-only, Hidden, and Archive.
But what if we don’t want to set it manually? What if we just want to check for the existence of an attribute and set it if it isn’t there? The next code snippet will check our file to see if the System attribute is set. If it isn’t, it will add it without changing any of the other existing attributes.
Const SYSTEM = 4
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.GetFile("C:boot.ini")
If NOT objFile.Attributes AND SYSTEM Then
objFile.Attributes = objFile.Attributes + SYSTEM
End If
We’ve taken a piece of our code that checks if a value is set and modified the conditional statement slightly. Instead of checking to see if it is set, we’re checking to see if it is NOT set. If it isn’t, then we add the value to the existing attribute value.
That wraps up this segment. Stick around for my next article. In the final part of this series we’ll learn how to use the FileSystemObject to write good error-checking to avoid unwanted errors when working with files and folders. We’ll also take a brief look at how to deal with files and folders on a remote machine. See you then. Until next time, keep coding!

| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |