Solved

Read-only - all files and folder in a folder

Posted on 2002-06-28
7
186 Views
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.

thanx
ExpertNot
0
Comment
Question by:ExpertNot
7 Comments
 
LVL 4

Expert Comment

by:kpkp
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])
     else
     if itemArray[n] is a directory
         then UpdateReadonlyFlag(itemArray[n])
  }
}
0
 
LVL 5

Accepted Solution

by:
rpai earned 200 total points
ID: 7116530
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7116533
To get the list:
http://www.mvps.org/vbnet/code/fileapi/recursive.htm

To change atributtes for a file use intrinsic SetAttr function
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7116536
rpai, you beat me!
0
 
LVL 28

Expert Comment

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

Private Sub Command1_Click()
Shell "command.com /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
0
 
LVL 26

Expert Comment

by:EDDYKT
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)
        Next
    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
        Next
    End If
End Sub


To call:

FileAttribute("c:\winnt")

0
 

Author Comment

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

thanx
ExpertNot
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now