• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2700
  • Last Modified:

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

0
eisenbergw
Asked:
eisenbergw
  • 2
  • 2
1 Solution
 
RobSampsonCommented:
Hi, this line:
If objFile.Attributes = objFile.Attributes AND 1 Then

should be
If objFile.Attributes AND 1 Then

Regards,

Rob.
0
 
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

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.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now