+ Reply to Thread
Results 1 to 9 of 9

[SOLVED] File Attributes

  1. #1
    mworthington@ntlworld.com
    Guest

    [SOLVED] File Attributes

    Hi,

    I am using the FileSystemObject to get a file (objImage), and need to
    determine whether is has both its Hidden and System attributes set.
    Help says :

    To determine which attributes are set, use the And operator to perform
    a bitwise comparison (a bit-by-bit comparison between identically
    positioned bits in two numeric expressions) of the value returned by
    the Attributes function and the value of the individual file attribute
    you want. If the result is not zero, that attribute is set for the
    named file.

    OK. So can someone please help me understand why the first 2 work, but
    the last 2 don't?!

    If objImage.Attributes And vbHidden Then MsgBox "vbHidden"

    If objImage.Attributes And vbSystem Then MsgBox "vbSystem"

    If objImage.Attributes And vbHidden And _
    objImage.Attributes And vbSystem Then MsgBox "vbHidden &
    vbSystem"

    If objImage.Attributes And vbHidden = vbHidden And _
    objImage.Attributes And vbSystem = vbSystem Then MsgBox
    "vbHidden & vbSystem"

    Any guidance is much appreciated!

    Cheers,

    Mark


  2. #2
    Bob Phillips
    Guest

    Re: File Attributes

    Hi Mark,

    Give this a whirl

    If (objImage.Attributes And vbHidden Or _
    objImage.Attributes And vbSystem) = vbHidden Or vbSystem Then

    or

    If (objImage.Attributes And vbHidden) + _
    (objImage.Attributes And vbSystem) = vbHidden + vbSystem Then

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    <mworthington@ntlworld.com> wrote in message
    news:1112043727.894715.197570@l41g2000cwc.googlegroups.com...
    > Hi,
    >
    > I am using the FileSystemObject to get a file (objImage), and need to
    > determine whether is has both its Hidden and System attributes set.
    > Help says :
    >
    > To determine which attributes are set, use the And operator to perform
    > a bitwise comparison (a bit-by-bit comparison between identically
    > positioned bits in two numeric expressions) of the value returned by
    > the Attributes function and the value of the individual file attribute
    > you want. If the result is not zero, that attribute is set for the
    > named file.
    >
    > OK. So can someone please help me understand why the first 2 work, but
    > the last 2 don't?!
    >
    > If objImage.Attributes And vbHidden Then MsgBox "vbHidden"
    >
    > If objImage.Attributes And vbSystem Then MsgBox "vbSystem"
    >
    > If objImage.Attributes And vbHidden And _
    > objImage.Attributes And vbSystem Then MsgBox "vbHidden &
    > vbSystem"
    >
    > If objImage.Attributes And vbHidden = vbHidden And _
    > objImage.Attributes And vbSystem = vbSystem Then MsgBox
    > "vbHidden & vbSystem"
    >
    > Any guidance is much appreciated!
    >
    > Cheers,
    >
    > Mark
    >




  3. #3
    mworthington@ntlworld.com
    Guest

    Re: File Attributes

    Hi Bob,

    There seems to be something strange with the Attribute property ...

    Put this code in a new workbook:

    Sub test()

    x = GetAttr(Range("A1").Parent.Parent.FullName)
    x = x + 34 ' Set Archive & Hidden attribute
    If x = vbNormal Then MsgBox "vbNormal" ' Note, AND doesn't work!
    If x And vbArchive = vbArchive Then MsgBox "vbArchive"
    If x And vbHidden Then MsgBox "vbHidden"
    If x And vbArchive And x And vbHidden Then MsgBox "vbArchive and
    vbHidden"
    If x And vbArchive = vbArchive And x And vbHidden = vbHidden Then
    MsgBox "vbArchive and vbHidden"
    x = 0 ' Clear Archive & Hidden attributes
    If x = vbNormal Then MsgBox "vbNormal" ' Note, AND doesn't work!
    If x And vbArchive Then MsgBox "vbArchive"
    If x And vbHidden Then MsgBox "vbHidden"

    End Sub

    It seems OK, apart from the 4th If statement, which doesn't work
    (whereas the 5th one does), which I don't understand.

    What's more, when I apply the 4th If statement to the attribute
    property of a file derived via the FileSystemObject, it actually says
    that NO files meet the requirements, and the 5th one says that ALL meet
    the requirements!

    This has got me. It sure looks like another Excel object model oddity.

    The workbook that I have the full FileSystemObject is rather large, so
    I can't paste the code. I could e-mail it off-line if you're interested
    ......

    Cheers,

    Mark


  4. #4
    mworthington@ntlworld.com
    Guest

    Re: File Attributes

    Hi Bob,

    There seems to be something strange with the Attribute property ...

    Put this code in a new workbook:

    Sub test()

    x = GetAttr(Range("A1").Parent.Parent.FullName)
    x = x + 34 ' Set Archive & Hidden attribute
    If x = vbNormal Then MsgBox "vbNormal" ' Note, AND doesn't work!
    If x And vbArchive = vbArchive Then MsgBox "vbArchive"
    If x And vbHidden Then MsgBox "vbHidden"
    If x And vbArchive And x And vbHidden Then MsgBox "vbArchive and
    vbHidden"
    If x And vbArchive = vbArchive And x And vbHidden = vbHidden Then
    MsgBox "vbArchive and vbHidden"
    x = 0 ' Clear Archive & Hidden attributes
    If x = vbNormal Then MsgBox "vbNormal" ' Note, AND doesn't work!
    If x And vbArchive Then MsgBox "vbArchive"
    If x And vbHidden Then MsgBox "vbHidden"

    End Sub

    It seems OK, apart from the 4th If statement, which doesn't work
    (whereas the 5th one does), which I don't understand.

    What's more, when I apply the 4th If statement to the attribute
    property of a file derived via the FileSystemObject, it actually says
    that NO files meet the requirements, and the 5th one says that ALL meet
    the requirements!

    This has got me. It sure looks like another Excel object model oddity.

    The workbook that I have the full FileSystemObject is rather large, so
    I can't paste the code. I could e-mail it off-line if you're interested
    ......

    Cheers,

    Mark


  5. #5
    mworthington@ntlworld.com
    Guest

    Re: File Attributes

    Bob,

    Meant to say, those 2 suggestions didn't help with my particular
    attribute problem.

    Cheers,

    Mark


  6. #6
    Bob Phillips
    Guest

    Re: File Attributes

    When you add 34 to set the archive and hidden attributes, you should check
    first that they are not already set, otherwise you distort it.

    Do

    x = x Or 34

    I don't think the problem is with attributes, but do you understand what AND
    and OR does?

    If x And (vbArchive Or vbHidden) Then _
    MsgBox "vbArchive and vbHidden"


    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    <mworthington@ntlworld.com> wrote in message
    news:1112092449.962666.209410@z14g2000cwz.googlegroups.com...
    > Hi Bob,
    >
    > There seems to be something strange with the Attribute property ...
    >
    > Put this code in a new workbook:
    >
    > Sub test()
    >
    > x = GetAttr(Range("A1").Parent.Parent.FullName)
    > x = x + 34 ' Set Archive & Hidden attribute
    > If x = vbNormal Then MsgBox "vbNormal" ' Note, AND doesn't work!
    > If x And vbArchive = vbArchive Then MsgBox "vbArchive"
    > If x And vbHidden Then MsgBox "vbHidden"
    > If x And vbArchive And x And vbHidden Then MsgBox "vbArchive and
    > vbHidden"
    > If x And vbArchive = vbArchive And x And vbHidden = vbHidden Then
    > MsgBox "vbArchive and vbHidden"
    > x = 0 ' Clear Archive & Hidden attributes
    > If x = vbNormal Then MsgBox "vbNormal" ' Note, AND doesn't work!
    > If x And vbArchive Then MsgBox "vbArchive"
    > If x And vbHidden Then MsgBox "vbHidden"
    >
    > End Sub
    >
    > It seems OK, apart from the 4th If statement, which doesn't work
    > (whereas the 5th one does), which I don't understand.
    >
    > What's more, when I apply the 4th If statement to the attribute
    > property of a file derived via the FileSystemObject, it actually says
    > that NO files meet the requirements, and the 5th one says that ALL meet
    > the requirements!
    >
    > This has got me. It sure looks like another Excel object model oddity.
    >
    > The workbook that I have the full FileSystemObject is rather large, so
    > I can't paste the code. I could e-mail it off-line if you're interested
    > .....
    >
    > Cheers,
    >
    > Mark
    >




  7. #7
    mworthington@ntlworld.com
    Guest

    Re: File Attributes

    Bob,

    I agree about the problem of distorting ....

    Oh yes, I understand bitwise comparisons ( (2 AND 32) = 0, (2 AND 34) =
    2 etc) and I think I understand attributes. "To determine which
    attributes are set, use the And operator to perform a bitwise
    comparison (a bit-by-bit comparison between identically positioned bits
    in two numeric expressions) of the 2 values. If the result is not zero,
    the attribute is set" etc. I believe that when bitwise comparing the
    hidden attribute (2) with any combination of hidden & (an)other
    attribute(s), it should still give a "true" result.

    That last sentence confuses me as much as the problem does! Anyway, I
    spent quite some time today experimenting, working with bitwise
    comparing numbers ... for example

    Sub test1()

    ' vbNormal = 0
    ' vbHidden = 2
    ' vbSystem = 4
    ' vbArchive = 32

    ' Bitwise comparison experiment, using the values 2, 4

    ' The <> 0 is optional ...

    If 2 And 6 <> 0 Then MsgBox "True"
    If 4 And 6 <> 0 Then MsgBox "True"

    ' The <> 0 is NOT optional ...

    ' This doesn't work!

    If 2 And 6 <> 0 And 4 And 6 <> 0 Then MsgBox "Both True"

    ' This DOES work!

    If (2 And 6) <> 0 And (4 And 6) <> 0 Then MsgBox "Both True"

    ' This doesn't work!

    If (2 And 6) And (4 And 6) Then MsgBox "Both True"

    ' This DOES work!

    If 6 And 2 <> 0 And 6 And 4 <> 0 Then MsgBox "Both True"

    ' This DOES work!

    If (6 And 2) <> 0 And (6 And 4) <> 0 Then MsgBox "Both True"

    End Sub

    So, it just seems that my problem is solved by using the ( ) in the
    "right" place ... but note the 3rd and 6th IF statements ... hmm?!

    So in my original post, where this didn't work

    With objImage
    If .Attributes And vbHidden And _ .Attributes And vbSystem Then
    MsgBox "vbHidden & vbSystem"
    End with

    this does

    With objImage
    If (.Attributes And vbHidden) = 0 And (.Attributes And vbSystem) =
    0 Then MsgBox "vbHidden & vbSystem"

    It still amazes me what I learn ... just the ( ) resolved this, but I
    still remain slightly confused by the "need" for the =0 when combining
    comparisons.....

    Thanks for the pointers in your last post.

    Cheers,

    Mark


  8. #8
    MP
    Guest

    Re: File Attributes


    <mworthington@ntlworld.com> wrote in message
    news:1112136842.213979.235650@f14g2000cwb.googlegroups.com...
    > Bob,


    > I
    > still remain slightly confused by the "need" for the =0 when combining
    > comparisons.....
    >
    > Thanks for the pointers in your last post.
    >
    > Cheers,
    >
    > Mark
    >


    maybe because....
    Debug.Print CInt(True)
    ?



  9. #9
    mworthington@ntlworld.com
    Guest

    Re: File Attributes

    MP,

    Yes, "The True keyword has a value equal to -1."

    But I'm still at a loss why

    If 2 And 6 <> 0 And 4 And 6 <> 0 Then Debug.Print "Both True"
    doesn't work but
    If -1 And -1 Then Debug.Print "Both True"
    If True And True Then Debug.Print "Both True"
    and
    If 6 And 2 <> 0 And 6 And 4 <> 0 Then Debug.Print "Both True"
    do work.

    Cheers,

    Mark


    > maybe because....
    > Debug.Print CInt(True)
    > ?



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1