Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Apr 12, 2002



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Clear File Attributes From Entire Directory Tree of Files

This function removes attributes from files in the folder you specify in strFolder. It also can walk down all subfolders of this folder recursively. This function was written mainly for removing the read-only flag that all files have after you copy a bunch from a CD (oldattribute = vbReadOnly), but you can use it with other attributes as well. Look for the other constants for file attributes in the object browser (subset scripting). To use this function, you must first set a Reference to the "Microsoft Scripting Runtime":
Option Explicit
Private fso As New Scripting.FileSystemObject
Public Function ClearAttribute(strFolder As _
String, fIncludeSubfolders As Boolean, _
oldAttribute As VbFileAttribute) As Long
Dim fld As Scripting.Folder
Dim subfld As Scripting.Folder
Dim file As Scripting.file
Dim lngCount As Long
Set fld = fso.GetFolder(strFolder)
For Each file In fld.Files
If file.Attributes And oldAttribute Then
file.Attributes = file.Attributes _
And Not oldAttribute
' Count the files
lngCount = lngCount + 1
End If
Next file
If fIncludeSubfolders Then
For Each subfld In fld.SubFolders
' Add total from this subfolder
' And its subfolders
lngCount = lngCount + _
ClearAttribute(subfld.Path, _
True, oldAttribute)
Next subfld
End If
' Number of total processed files
ClearAttribute = lngCount
End Function
Hans Weichselbaumer
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date