Login   /   Register

Change a read-only file to a read-write file

Rate this article
     1 votes, average: 5 out of 51 votes, average: 5 out of 51 votes, average: 5 out of 51 votes, average: 5 out of 51 votes, average: 5 out of 5
Loading ... Loading ...
March 31st, 2008 by daniva
Option Explicit
 
Const ReadOnly = 1
Dim fso, file
Dim filePathStr
 
filePathStr = "C:\MyFile.txt"
Set fso = CreateObject( "Scripting.FileSystemObject" )
If fsoutil.FileExists( filePathStr ) Then
    Set file = fso.GetFile( filePathStr )
    If file.Attributes And ReadOnly Then
        file.Attributes = file.Attributes Xor ReadOnly
    End If
Else
    Reporter.ReportEvent micWarning, "Change Attributes", "File -> " & filePathStr & " was not found."
End If

Begin by setting the value of the constant ReadOnly to 1. We then create an instance of the FileSystemObject, and use the GetFile method to bind to the file filePathStr.

The Read-only attribute is stored as part of a "bitmask" along with other file attributes such as Hidden (value of 2, which indicates whether the file is a hidden file) and System (value of 4, which indicates whether the file is a system file). In a bitmask individual attributes can be likened to switches, switches that can be either on or off. If the switch with value of 1 is on, then the file is read-only; if the switch with the value of 1 is off, then the file is read-write

So, how you determine whether one of these switches is on, or off? The simple answer is that you use the bitwise And operator. Notice the following odd-looking line in the script.

If file.Attributes And ReadOnly Then

This line is checking to see if the ReadOnly attribute (with a value of 1) is on.

What if we wanted to see if the file was a hidden file? Well, in that case, we’d use this line of code.

If file.Attributes And Hidden Then

Basically the And operator can be read like this: "If we’re looking at the Attributes of the file and of the ReadOnly switch is on, then this is a read-only file and this If statement is True. If the ReadOnly switch is off, then this If statement is False."

So why are we checking to see if the file is read-only? Well, momentarily, we’re going to use the Xor operator to "flip" the switch; that’s what this line of code is for?

file.Attributes = file.Attributes Xor ReadOnly

In this example, Xor simply toggles the file from one state to the other. If the file is read-only, Xor switches it to read-write; if it’s read-write, Xor switches it to read-only. That’s why we first check to see if the file is already read-only. If it is, we want to flip the switch and make it read-write. If it’s already read-write, though, we obviously don’t want to flip the switch; after all, that would make the file read-only.

here’s a script which binds to the folder pathStr and turns all the read-only files in that folder into read-write files.

Option Explicit
 
Const ReadOnly = 1
Dim fso, file, files, folder
Dim pathStr
 
pathStr = "C:\MyFolder"
Set fso = CreateObject( "Scripting.FileSystemObject" )
Set folder = fso.GetFolder( pathStr )
Set files = folder.Files
For Each file In files
    If file.Attributes And ReadOnly Then
        file.Attributes = file.Attributes Xor ReadOnly
    End If
Next

Based on "How Can I Change a Read-only File to a Read-write File?" from the Scripting Guys.

Posted in FileSystemObject

One Response to “Change a read-only file to a read-write file”

  1. Balaji_singaram Says:

    [+]

    hai daniva,there is a small change in the script u created a object in the name of "fso" but u r checking with fsoutil.exists in t... ...

Leave a Reply

You must be logged in to post a comment.

This article was viewed 501 times