Solved Removing Attributes (1 Viewer)

jazsriel

Member
Local time
Today, 03:45
Joined
Dec 21, 2020
Messages
62
Hi,

Is there a way to remove read only attributes from all files in a folder using VBA?

I found this code and it works but only if I point it to a specific file. My file names are constantly changing in the folders I need to remove these from.

Dim fs As Variant, f As Variant
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile("Filepath here")
f.Attributes = f.Attributes - (f.Attributes And (vbReadOnly + vbArchive))
Set fs = Nothing
Set f = Nothing
 

CJ_London

Super Moderator
Staff member
Local time
Today, 09:45
Joined
Feb 19, 2013
Messages
16,612
never had to do this so just a couple of guesses

1. change the folder attributes instead of the file

or

2. modify your code to loop through the folder files - perhaps something like (aircode, not tested)

Code:
function changeAttributes(fldr as string)
Dim fs As Variant, f As Variant
dim fname as string
Set fs = CreateObject("Scripting.FileSystemObject")
fname=dir(fldr)
while fname<>""
    Set f = fs.GetFile(fldr & "\" & fname)
    f.Attributes = f.Attributes - (f.Attributes And (vbReadOnly + vbArchive))
    fname=dir
wend
Set fs = Nothing
Set f = Nothing
end function
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:45
Joined
May 7, 2009
Messages
19,243
another alternative:
Code:
Function changeAttributes(fldr As String)
    Dim cmd As String
    fldr = Replace$(fldr & "\", "\\", "\") & "*.*"
    cmd = "c:\windows\system32\attrib.exe -r -a " & fldr
    Call Shell(cmd, vbHide)
End Function
 

jazsriel

Member
Local time
Today, 03:45
Joined
Dec 21, 2020
Messages
62
I am not sure how to call a function with a string in parenthesis.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:45
Joined
May 7, 2009
Messages
19,243
to change attributes to all files on your Document (example)

Call changeAttributes("C:\Users\Jazriel\Documents")
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:45
Joined
Feb 28, 2001
Messages
27,186
When doing Boolean operations, the correct form might be more like

Code:
function changeAttributes(fldr as string)
Dim fs As Variant, f As Variant
dim fname as string
Set fs = CreateObject("Scripting.FileSystemObject")
fname=dir(fldr)
while fname<>""
    Set f = fs.GetFile(fldr & "\" & fname)
    'f.Attributes = f.Attributes - (f.Attributes And (vbReadOnly + vbArchive))
    f.Attributes =f.Attributes AND ( NOT ( vbReadOnly OR vbArchive ) )
    fname=dir
wend
Set fs = Nothing
Set f = Nothing
end function

The problem with doing the math operators is that they will use 2's-complement arithmetic but bit-twiddling doesn't work quite as well when following 2's complement integer rules. And, unfortunately for this case, Windows runs on an Intel or Intel-like box that uses 2's complement math.

For those not up on Boolean operations:

The ( vbReadOnly OR vbArchive ) creates an integer bit stream with the two attribute flags set i.e. bitwise TRUE. The NOT flips all of the bits so you have all possible bit flags EXCEPT the two named attribute flags. The AND "masks" off the missing bits and leaves the other bits alone.
 

jazsriel

Member
Local time
Today, 03:45
Joined
Dec 21, 2020
Messages
62
Thank you all for the help on this and being patient with my lack of knowledge. These solutions do accomplish what I was trying to do, thank you.
 

Users who are viewing this thread

Top Bottom