Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, How would I efficiently inco 1

Status
Not open for further replies.

adamrobert

Programmer
Oct 15, 2003
5
GB
Hi,

How would I efficiently incorporate an OR statement into the code below:

Const READONLY = 1
Const WRITEABLE = 0

If file.Attributes = READONLY ........OR .......Then
file.Attributes = WRITEABLE

I've discovered that all file values are not necessarily 1 when they are of type 'read only' therefore I need to account for other file types such as 32 or 33.

Any help would be greatly appreciated.

Cheers,

adamrobert
 
Try something like this:
Code:
If file.Attributes And READONLY Then
  file.Attributes = file.Attributes - READONLY
End If

Hope This Help
PH.
 
Hello adamrobert & PHV,

I don't think adamrobert's problem is well-posed and consequently PHV's solution is of no generic value. That is too bad.

A generic use of flag with bit masking to retrieve its on-off can be illustrate in a fictitious system as follows.

Suppose a file attribute is a 4-bit flag with bit-0 systemfile, bit-1 zipfile, bit-2 readable and bit-3 writeable.
Code:
Const IsSystem = 1
Const IsZip = 2
Const IsReadable = 4
Const IsWriteable = 8

ReadWriteFile = IsReadable Or IsWriteable   'yes, it is Or.

fileattribute = 1+2+8   'ie = 11
wscript.echo "The file is readwriteable : " & ((fileattribute And ReadWriteFile)=ReadWriteFile)
wscript.echo "The file is a system file : " & ((fileattribute And IsSystem)=IsSystem)

file attribute = 2+4+8   'ie = 14
wscript.echo "The file is readwriteable : " & ((fileattribute And ReadWriteFile)=ReadWriteFile)
wscript.echo "The file is a zipped file : " & ((fileattribute And IsZip)=IsZip)
It's all based on any number between 0 and 15 can uniquely decomposed into the base (1,2,4,8) and the retrievable of the property flag via
(fileattribute And IsProperty = IsProperty)
being true.

Of course, all these are just a rephrase of basic materials. But, it is all put into practice so that generic behavior and construction can be illustrated by the above that I write here off-hand.

regards - tsuji
 
Hmm...

If this
Code:
file
is an instance of the
Code:
File
object from the FileSystemObject's object model, the
Code:
Attributes
property's value is indeed a bit mask.

To test for the subvalue
Code:
ReadOnly
(1, in other words bit 0 set) in VBScript you simply use standard masking as tsuji suggests. Given a constant
Code:
READONLY
set to 1:
Code:
If (file.Attributes And READONLY) <> 0 Then
In order to turn off the READONLY bit without altering the others, you simply:
Code:
   file.Attributes = file.Attributes And Not READONLY
Or:
Code:
   file.Attributes = file.Attributes - READONLY
In this case these both yield the same result, though the first is &quot;more correct.&quot; For example, if
Code:
Attributes
is not tested for
Code:
ReadOnly
before performing the subtraction you will have messed things up.

If all you want to do is make sure the file is read only, you don't need to test the
Code:
Attributes
first, just use:
Code:
file.Attributes = file.Attributes And Not READONLY
It doesn't get any easier than this.

I am embarrassed for Microsoft that their FSO documentation even shows subtraction being used like this. It is the very height of &quot;bad form.&quot; I only mention it here at all because the scripting documentation shows it.
 
*Sigh*

I guess I can see how documentation gets messed up. I can't type straight myself.

Where I said:

If all you want to do is make sure the file is read only

I meant to say:

If all you want to do is make sure the file is not read only
 
Hello dilettante,

Does it appear in documentation? I must have paid not enough attention. Besides, vbs documentation like vba etc are known to have plenty of inaccuracy slipped in. But, I know how difficult to get a document perfect... and it gives good info for people to get-by.

regards - tsuji
 
From my official M$ documentation:
Code:
Function ToggleArchiveBit(filespec)
   Dim fso, f
   Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
   Set f = fso.GetFile(filespec)
   If f.attributes and 32 Then
      f.attributes = f.attributes - 32
      ToggleArchiveBit = &quot;Bit d'archive effacé.&quot;
   Else
      f.attributes = f.attributes + 32
      ToggleArchiveBit = &quot;Bit d'archive défini.&quot;
   End If
End Function
Sorry for the locale, but I'm a french guy.
 
Bonjour PHV,

You're right. I read again that part, still do not like it. I must have been very forgiving because I just swept that piece of code out of my memory. So my regret should go to m$.

mes respects - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top