need help with removing read-only file attribute

Hi -

I'm trying to delete a file via vbscript, but I'm getting stumped by something that should be easy. As you can see from my code snippet, I want to make sure that the old file I want to remove is not read-only. However,  the line "If objFile.Attributes = objFile.Attributes AND 1 Then" always seems to be evaluated true, because the read-only attribute gets flipped, whether it was originally set or not. (The MsgBox lines are just tracing for me while I troubleshoot.)

What am I doing wrong?

Thanks,
Wayne
' remove old pcf file
' make sure if there is an existing file it is not read-only so that DeleteFile works
 
	If objFSO.FileExists(sOldFile) Then
		Set objFile = objFSO.GetFile(sOldFile)
		If objFile.Attributes = objFile.Attributes AND 1 Then
			objFile.Attributes = objFile.Attributes XOR 1 
			MsgBox "fixed read-only old file"
		Else
			MsgBox "not read-only old file"
		End If
		objFSO.DeleteFile(sOldFile)
		MsgBox "old pcf deleted"
	Else
		MsgBox "old file not previously existing"
	End If

Open in new window

eisenbergwAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi, this line:
If objFile.Attributes = objFile.Attributes AND 1 Then

should be
If objFile.Attributes AND 1 Then

Regards,

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eisenbergwAuthor Commented:
Thanks.
0
eisenbergwAuthor Commented:
Thanks. Sometimes you miss stuff like that. Can you clarify for me the structure of that statement? What does objFile.Attributes *AND* <some number here> mean? It's the first time I've seen that. I would assume that I could also use other words like NOT or NOR. When would you use that kind of a statement?
0
RobSampsonCommented:
Hi, from here:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/jan05/hey0118.mspx

"The way we determine whether or not a file is read-only is by checking the appropriate switch in the Attributes bitmask. If this switch (which has the value of 1; thats why we created a constant named READ_ONLY and assigned it the value 1) is enabled, then the file is read-only. If the switch is not enabled then the file is writeable.

This line of code is a bit confusing (welcome to the world of Boolean logic!) but it can be read like this: if the read-only switch within the Attributes bitmask is enabled, then run the next line of code. The syntax is kind of clumsy-looking, but its just something we have to live with.

To set the file back to read-only, in that case, wed want to determine if the read-only switch is not set. Thus wed use this line of code instead:

If Not objFile.Attributes AND READ_ONLY Then

If it turns out that we have a read-only file, the script then uses the Boolean operator XOR to flip the read-only switch. This line of code executes only if the read-only switch is enabled; consequently, its going to turn this switch off, thus making the file writeable. The XOR operator just flips things from on to off and vice-versa. Thats why we check to see if the read-only switch is enabled before we call XOR. Suppose we didnt check and suppose the file was writeable. In that case, XOR would flip the switch, and make our writeable file read-only."


I hope that helps make it clearer.  It's the way things like Attributes and UserAccountControl flags work, I think because they're groups of boolean flags.... I don't quite understand why they're built that way myself, but we can still use it ;-)

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.