Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Read-only - all files and folder in a folder

Posted on 2002-06-28
Medium Priority
Last Modified: 2012-05-04
Hi experts,

suppose i want to make all files in a folder read-only (or the other way around not-read-only), how can i do this with code?
There can be several levels of folders with files in them.

Question by:ExpertNot
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 7116519
You need to write a recursive function.  

The function accepts a folder name as a parameter.  Then finds all the files and folders in that folder.  Updates the attributes of all the files found, and then the functions calls itself for each folder found, passing in the name of one of the folders found.

some _pseudo_ code:

function UpdateReadonlyFlag(string folderName)
  itemArray[] = GetListOfFilesAndFolders(folderName);
  for n = 1 to numberOfItemsFound
     if itemArray[n] is a file
         then UpdateReadOnlyFlag(itemArray[n])
     if itemArray[n] is a directory
         then UpdateReadonlyFlag(itemArray[n])

Accepted Solution

rpai earned 800 total points
ID: 7116530
LVL 16

Expert Comment

ID: 7116533
To get the list:

To change atributtes for a file use intrinsic SetAttr function
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 16

Expert Comment

ID: 7116536
rpai, you beat me!
LVL 28

Expert Comment

ID: 7116567
Not the greatest way,but you could use:

Private Sub Command1_Click()
Shell " /c attrib +r D:\Test\*.* /s", vbHide
End Sub

change the +r to -r to remove the read only attribute.
The /s will change attributes on files in sub directories
LVL 26

Expert Comment

ID: 7116729

Public Sub FileAttribute(FileName)
    Dim fso
    Dim folder
    Dim f
    Dim Files
    Dim File
    Dim SubFolders
    Dim SubFolder
    On Error Resume Next
    If (Dir$(FileName, vbDirectory) = "") Then Exit Sub
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(FileName)
    Set SubFolders = folder.SubFolders
    If SubFolders.Count <> 0 Then
        For Each SubFolder In SubFolders
            FileAttribute (SubFolder)
    End If
    Set Files = folder.Files
    If Files.Count <> 0 Then
        For Each File In Files
            Set f = fso.GetFile(File)
            f.Attributes = f.Attributes or 1
            Set f = Nothing
    End If
End Sub

To call:



Author Comment

ID: 7133643
sorry this took so long,
had the flu.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question