Set File Attributes - Read Only or Read Write - March 2004

Sometime ago I wrote functions to set a file's read only attribute. I recently revisited the code, added some functions and properties and put it all in a class module I call 'clsReadWrite'. It becomes the March 2004 tip-of-the-month.

Demo Program

I wrote a VB6 demo program to illustrate the use of the class. The demo:


VB6 Demo

File Name - A fully qualified file name. The file to be examined or whose read/write attribute is to be changed. File: SampleFileForReadWriteTip.txt is distributed with the tip and is the default file shown in the demo.

Browse Button - Click the browse button [button whose caption is 3 dots] to retrieve a file name.

File Attributes - The file's attributes: Read Only or Read and Write; Hidden, Archive, System.

IsFile... Command Button - Click this button to retrieve the file's attributes. Actually this is done by the demo each time you change the file name.

SetFileReadOnly - Click this button to change the file attribute to Read Only. Retain other attributes.

SetFileReadWrite - Click this button to change the file attribute to 'Normal'. Retain other attributes.

Sample Calling Code

Private Sub Command2_Click(Index As Integer)
  Dim o As New clsReadWrite

  On Error GoTo Command2_ClickEH
  With o
    .sFileName = Me.Text1.Text
    Select Case Index
      Case 0 'isreadonly
        ...
      Case 1 'set read only
        If .SetFileReadOnly() Then
          ...
        End If
      Case 2 'set read/write
        If .SetFileReadWrite() Then
          ...
        End If
      End Select
  End With
  Exit Sub
Command2_ClickEH:
  MsgBox Err.description, vbExclamation, "Error: " & Err.Number
End Sub

Class clsReadWrite Methods and Properties


Properties and Methods

Methods/Properties Operands Sample
.IsFileReadOnly [sFileName] If .IsFileReadOnly() Then
.IsFileArchive [sFileName] If .IsFileArchive() Then
.IsFileHidden [sFileName] If .IsFileHidden() Then
.IsFileSystem [sFileName] If .IsFileSystem() Then
.FileName Fully Qualified File Name .FileName = "c:\MyFolder\MyFie.txt"
.SetFileReadOnly [sFileName] .SetFileReadOnly - Always returns True
.SetFileReadWrite [sFileName] .SetFileReadWrite - Always returns True
iAttr File attributes returned by the 'set' functions.  

The Demo Program Uses These Modules

Module - Click for More Info Description Used in the Demo To
clsReadWrite.cls Retrieve and set attributes The subject of this tip.
Include in your application.
cDialog.cls Browse for file by Francesco Balena Browse for file.
modFileExistsEX.bas Check for a file's existence Set the command buttons enabled if the file exists.
modAddBackSlash.bas Add a Backslash if Needed Add a backslash to App.Path, if needed.
modConnectToWebSite.bas Connect to a web site Show this info when Help/Info is
selected from the demo.
modInfoAbout.bas DoInfo, DoAbout, DoTip Link to this page and provide helpful information

NTFS Compressed Files

The NT File System [NTFS] supports automatic compression of files. A compressed file is displayed with a 'C' attribute in Windows Explorer. VB6 does not support the updating of this attribute. So the SetAttr method will fail if you first use GetAttr, 'or' a setting to the retrieved attribute then try to use SetAttr to update the attribute. The routines provided via this tip avoid setting the compression attribute.

Download VB Code

Click here to download the VB 6 source code [16k].

Previous Tip   Tip Index   Next Tip

Home

Updated 2004/11/21