0

I tried that to add an attribute to a file/folder

Get-ChildItem '$PATH' -Recurse | foreach{$_.Attributes = $_.Attributes + 'Compressed'}

Problem is that it create a cycle like that : Not compressed > Compressed > Compressed, Offline > Compressed, NotContentIndexed > Compressed, Offline, NotContentIndexed

So the command should work fine on any element on $PATH that aren't compressed at all, but if there is the attribute, it will add/delete Offline, NotContentIndexed to the attributes (Compressed and any other irrelevant attributes stays fine), considering that I never asked for that. I only want to add/let Compressed without touching anything else

5
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Mar 4, 2022 at 12:59
  • Last line of the post states "I only want to add/let Compressed without touching anything else"
    – aac
    Commented Mar 4, 2022 at 13:00
  • from what i can tell you need to give it an ARRAY of the items, not a string.
    – Lee_Dailey
    Commented Mar 4, 2022 at 18:14
  • I don’t fully understand what’s happening — is the sequence you show really what you’re getting? — and I don’t know PowerShell. But it looks like Attributes is a bitmap, and you don’t want to do ordinary arithmetic on a bitmap; you need a logical operation. Can you do $_.Attributes = $_.Attributes | 'Compressed'? Commented Mar 5, 2022 at 6:07
  • You can test if a file or folder is compressed or not using something like (Get-Item -Path 'X:\theFileOrFolder').Attributes -band 'Compressed' If that returns 'Compressed', then... it is compressed otherwise it is not. Is that what you mean? (-band stands for Binary AND and in this case it test for the bitmap including the value 2048 or not. 'Compressed' is easier to write than the numeric value and PowerShell will understand that)
    – Theo
    Commented Mar 5, 2022 at 21:13

0

You must log in to answer this question.