Assigning NTFS rights to files

Hi Experts, I am developing an application that requires to configure the rights (for instance only read access) to specified files that are stored in the database based on the login ID. In the database, it has been setup for each login ID, the specified files with its assoicated rights. Now my problem is to be able to set this rights to the files accordingly as specified in the database.

Who is Participating?
RainUKConnect With a Mentor Commented:
Okay go to this link;en-us;316440

Download the SetPerm.exe

In the module SetPerm.Bas look at the public function named UpdatePermissionsOfFolder.
But note that the folder/file must already exist before you set the permissions on it!!!

xcacls.exe (a microsoft command line utility)

you can use shell to call it. or wscript.
You need to use these APIs

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

bpyeoAuthor Commented:
Hi RainUK, do you have any code sample for your recommendation.

bpyeoAuthor Commented:
Hi RainUK, I have visited the a/m URL and it is mainly to change the owner of a file/folder and not setting the rights to file/folder. Please advice on the setting of rights (for instance, only read access) to file/folder.

Guaranteed this is the easiest way to set file attributes Using FileSystemobject. you need to play with it though, test good because you can get the attributes messed up, for instance if  a file is readonlly and you set system on it, it could remove readonly so you have to set it to 5(4+1) for read only and system.

Attributes Property
See Also    Example    Applies To    Specifics


Sets or returns the attributes of files or folders. Read/write or read-only, depending on the attribute.


object.Attributes [= newattributes]

The Attributes property has these parts:

Part Description
object Required. Always the name of a File or Folder object.
newattributes Optional. If provided, newattributes is the new value for the attributes of the specified object.


The newattributes argument can have any of the following values or any logical combination of the following values:

Constant     Value          Description
Normal         0              Normal file. No attributes are set.
ReadOnly      1             Read-only file. Attribute is read/write.
Hidden          2             Hidden file. Attribute is read/write.
System         4             System file. Attribute is read/write.
Volume         8             Disk drive volume label. Attribute is read-only.
Directory       16           Folder or directory. Attribute is read-only.
Archive         32           File has changed since last backup. Attribute is read/write.
Alias             64           Link or shortcut. Attribute is read-only.
Compressed  128         Compressed file. Attribute is read-only.

The following code illustrates the use of the Attributes property with a file:

Sub SetClearArchiveBit(filespec)
    Dim fs, f, r
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(fs.GetFileName(filespec))
    If f.attributes and 32 Then
        r = MsgBox("The Archive bit is set, do you want to clear it?", vbYesNo, "Set/Clear Archive Bit")
        If r = vbYes Then
            f.attributes = f.attributes - 32
            MsgBox "Archive bit is cleared."
            MsgBox "Archive bit remains set."
        End If
        r = MsgBox("The Archive bit is not set. Do you want to set it?", vbYesNo, "Set/Clear Archive Bit")
        If r = vbYes Then
            f.attributes = f.attributes + 32
            MsgBox "Archive bit is set."
            MsgBox "Archive bit remains clear."
        End If
    End If
End Sub

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.