Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Getting Copy-Item to display messages (like the old COPY command in CMD.EXE) ??

Reply
 
Old 10-24-2007   #11 (permalink)
Bob Landau


 
 

Re: Getting Copy-Item to display messages (like the old COPY comma

I REALLY appreciate you sticking with me on this Jacques but I still having
problems trying to get the behavior I want:

Compatibility with NT's DIR command

with the much more simple clause that you have shown

($_.Attributes -band 33) -eq 33

Rather than trying to explain this in words I'd like to show you

@"
"hello world" > x
CMD /C Attrib +a +r x
Copy x y
Copy x z
CMD /C Attrib +h z
CMD /C DIR /aar *
Get-ChildItem . | Where { (`$_.Attributes -band 33) -eq 33 }
"@ > test.ps1

Run this script

..\test

DIR . /aar will show the files x,y,z

where as

Get-ChildItem . | Where { (`$_.Attributes -band 33) -eq 33 }

will only show x and y.

If I'm mis-understanding the pipeline expression I'd appreciate you showing
it to me again because the above is not compatible

This clause

Where { (`$_.Attributes -band 33) -eq 33 }

was along the lines that I first took but the problem is the second logical
clause is comparing for equality not "at least"

thx
bob

"Jacques Barathon [MS]" wrote:
Quote:

> "Bob Landau" <BobLandau@xxxxxx> wrote in message
> news:6CE1A1EC-2FF8-4CE9-A6A6-593EDF30EC0E@xxxxxx
Quote:

> >I wasn't aware directories would never have the archive bit set but they
> >can
> > have read, hidden and system (c:\windows\assembly is one of many
> > directories
> > other attributes set.
> >
> > At least one reason that it's convoluted is to keep compatibility with how
> > "CMD /C DIR" processes attribues. This will not work.
> >
> > ($_.Attributes -band 33) -eq 33
>
> Have you tested it?
>
Quote:

> > because DIR only cares whether these attributes are set or not. For
> > instance
> > if all I want is the Readonly/System files in the Windows directory it
> > will
> > return any files that have these bits set. Any other attribute can also
> > be
> > set and it will still be consider a match so equality can't be used.
>
> Yes it can. I am not testing Attributes as being equal to 33, I am testing
> the value as having "at least" the bits that make 33 as a combination of
> possible attributes. If a file has other attributes it will also be
> recognised as positive to the match.
>
> Does that make sense?
>
> Jacques
>
>

My System SpecsSystem Spec
Old 10-24-2007   #12 (permalink)
Jacques Barathon [MS]


 
 

Re: Getting Copy-Item to display messages (like the old COPY comma

"Bob Landau" <BobLandau@xxxxxx> wrote in message
news:33B679C4-9543-4F79-A2EE-792A811C075B@xxxxxx
Quote:

>I REALLY appreciate you sticking with me on this Jacques but I still having
> problems trying to get the behavior I want:
....
Quote:

> Run this script
>
> .\test
>
> DIR . /aar will show the files x,y,z
>
> where as
>
> Get-ChildItem . | Where { (`$_.Attributes -band 33) -eq 33 }
>
> will only show x and y.
This is because by default get-childitem will not enumerate hidden files or
folders. Since you have activated the Hidden attribute on z, get-childitem
will only show x and y. Add -force to get-childitem and you will get what
you want.
Quote:

> If I'm mis-understanding the pipeline expression I'd appreciate you
> showing
> it to me again because the above is not compatible
>
> This clause
>
> Where { (`$_.Attributes -band 33) -eq 33 }
>
> was along the lines that I first took but the problem is the second
> logical
> clause is comparing for equality not "at least"
Right, but let's take this from the beginning:

$_.Attributes -band 33

=> this will apply a binary AND between the attributes of a file and 33
which corresponds to Read only and Archive. The binary AND will result in
bits in common being flagged (set to 1). Bits that are either in the left
term or in the right term but not in both will be set to 0. The net result
is that if a file's attributes *include* Read only and Archive, it will have
those bits in common with the reference value of 33 and therefore the result
of the operation will be 33.

If a file has only one of those two attributes, or none of them, the binary
AND will only flag those bits in common and will likely result in a lesser
value than 33 (down to zero if no attribute is in common).

It doesn't matter if a file has other attributes than those two: the binary
operation won't catch the corresponding bits and those will remain set to 0
in the result.

Now, we move on to the "equality":

($_.Attributes -band 33) -eq 33

=> what we want to verify here is that only the files where the binary AND
flagged the Read only and Archive attributes are being captured. Since we
saw above that the expected result of the binary AND is 33, and that no
other value will describe what we want, that is exactly the equality we are
verifying here.

Hope I am making sense. Let me know if that's not clear enough.

Regards,
Jacques

My System SpecsSystem Spec
Old 10-24-2007   #13 (permalink)
Keith Hill [MVP]


 
 

Re: Getting Copy-Item to display messages (like the old COPY comma

"Bob Landau" <BobLandau@xxxxxx> wrote in message
news:8AB72523-4133-4912-AAC5-CA4D2A109F41@xxxxxx
Quote:

> Ok in regards to Get-ChildItem/Copy-Item or any of these other generalized
> cmdlets I'm really not frustrated. I just find them awkward to use for
> "typical" file system maintance.
>
> Your example on how to prevent a file from being silently overwritten is
> an
> example. Yes it is a "one-liner" but so is CMD's
>
> copy *.txt \archive
>
> and "copy" will warn you if it's about to clobber a file.
>
> Here is another example using Get-ChildItem; say I'd like to find all
> files
> that have their archive and read-only bit set and order these by Creation
> time and then by name. For CMDs "dir" its done this way
>
> dir c:\junk /aar-d /o-dn /tc
>
> however to do the same thing for GCI you need a longer line even if you do
> use the shortcut aliases
>
> gci "c:\junk" | % { $attr = $( $_.get_Attributes() ); if ( -not ( (
> $attr
> -bxor 33 ) -band 33 ) -and -not ( $attr -band 16 ) ) { $_} } | Sort
> @{e={$_.CreationTime}; Descending=$true}, @{e={$_.Name}; Ascending=$true}
FYI we have some enhanced dir functions (dirx, dirs, dirt, dirw) in the
PowerShell community extensions, where you can do this:

dirx -force -recurse -setattr Readonly,Archive | Sort @{e={$_.CreationTime};
Des=$true}, @{e={$_.Name}; Asc=$true}

Execute dirx -? to get a list of all attributes you can use with -setAttr

--
Keith
http://www.codeplex.com/powershellcx

My System SpecsSystem Spec
Old 10-24-2007   #14 (permalink)
Bob Landau


 
 

Re: Getting Copy-Item to display messages (like the old COPY comma

Thanks Keith,

I have started to look at the Community Extensions last week ( I wanted to
get a good handle on Powershell first). Frankly if I had look at this site
sooner I likely would not have written my script in the first place. On the
flip side I wouldn't have learned as much about PS as I know now.

DirX is more or less the same thing which I've submitted to the Community
Extensions. The main difference being that the syntax follows exactly the
DIR command; whether thats a positive or negative depends on your perspective.
The other difference is it can be used within a pipeline

thx
bob

"Keith Hill [MVP]" wrote:
Quote:

> "Bob Landau" <BobLandau@xxxxxx> wrote in message
> news:8AB72523-4133-4912-AAC5-CA4D2A109F41@xxxxxx
Quote:

> > Ok in regards to Get-ChildItem/Copy-Item or any of these other generalized
> > cmdlets I'm really not frustrated. I just find them awkward to use for
> > "typical" file system maintance.
> >
> > Your example on how to prevent a file from being silently overwritten is
> > an
> > example. Yes it is a "one-liner" but so is CMD's
> >
> > copy *.txt \archive
> >
> > and "copy" will warn you if it's about to clobber a file.
> >
> > Here is another example using Get-ChildItem; say I'd like to find all
> > files
> > that have their archive and read-only bit set and order these by Creation
> > time and then by name. For CMDs "dir" its done this way
> >
> > dir c:\junk /aar-d /o-dn /tc
> >
> > however to do the same thing for GCI you need a longer line even if you do
> > use the shortcut aliases
> >
> > gci "c:\junk" | % { $attr = $( $_.get_Attributes() ); if ( -not ( (
> > $attr
> > -bxor 33 ) -band 33 ) -and -not ( $attr -band 16 ) ) { $_} } | Sort
> > @{e={$_.CreationTime}; Descending=$true}, @{e={$_.Name}; Ascending=$true}
>
> FYI we have some enhanced dir functions (dirx, dirs, dirt, dirw) in the
> PowerShell community extensions, where you can do this:
>
> dirx -force -recurse -setattr Readonly,Archive | Sort @{e={$_.CreationTime};
> Des=$true}, @{e={$_.Name}; Asc=$true}
>
> Execute dirx -? to get a list of all attributes you can use with -setAttr
>
> --
> Keith
> http://www.codeplex.com/powershellcx
>
My System SpecsSystem Spec
Old 10-24-2007   #15 (permalink)
Bob Landau


 
 

Re: Getting Copy-Item to display messages (like the old COPY comma

Thank you for being patient with me. I had to look back on my previous code
version to see what provoked me to go the path I did. Some logic flaw which I
didn't see caused me to switch to my direction.

I've now changed this to model what you've been telling me which is much
simpler to read.

thx
bob

"Jacques Barathon [MS]" wrote:
Quote:

> "Bob Landau" <BobLandau@xxxxxx> wrote in message
> news:33B679C4-9543-4F79-A2EE-792A811C075B@xxxxxx
Quote:

> >I REALLY appreciate you sticking with me on this Jacques but I still having
> > problems trying to get the behavior I want:
> ....
Quote:

> > Run this script
> >
> > .\test
> >
> > DIR . /aar will show the files x,y,z
> >
> > where as
> >
> > Get-ChildItem . | Where { (`$_.Attributes -band 33) -eq 33 }
> >
> > will only show x and y.
>
> This is because by default get-childitem will not enumerate hidden files or
> folders. Since you have activated the Hidden attribute on z, get-childitem
> will only show x and y. Add -force to get-childitem and you will get what
> you want.
>
Quote:

> > If I'm mis-understanding the pipeline expression I'd appreciate you
> > showing
> > it to me again because the above is not compatible
> >
> > This clause
> >
> > Where { (`$_.Attributes -band 33) -eq 33 }
> >
> > was along the lines that I first took but the problem is the second
> > logical
> > clause is comparing for equality not "at least"
>
> Right, but let's take this from the beginning:
>
> $_.Attributes -band 33
>
> => this will apply a binary AND between the attributes of a file and 33
> which corresponds to Read only and Archive. The binary AND will result in
> bits in common being flagged (set to 1). Bits that are either in the left
> term or in the right term but not in both will be set to 0. The net result
> is that if a file's attributes *include* Read only and Archive, it will have
> those bits in common with the reference value of 33 and therefore the result
> of the operation will be 33.
>
> If a file has only one of those two attributes, or none of them, the binary
> AND will only flag those bits in common and will likely result in a lesser
> value than 33 (down to zero if no attribute is in common).
>
> It doesn't matter if a file has other attributes than those two: the binary
> operation won't catch the corresponding bits and those will remain set to 0
> in the result.
>
> Now, we move on to the "equality":
>
> ($_.Attributes -band 33) -eq 33
>
> => what we want to verify here is that only the files where the binary AND
> flagged the Read only and Archive attributes are being captured. Since we
> saw above that the expected result of the binary AND is 33, and that no
> other value will describe what we want, that is exactly the equality we are
> verifying here.
>
> Hope I am making sense. Let me know if that's not clear enough.
>
> Regards,
> Jacques
>
>
My System SpecsSystem Spec
Old 10-26-2007   #16 (permalink)
Jacques Barathon [MS]


 
 

Re: Getting Copy-Item to display messages (like the old COPY comma

"Bob Landau" <BobLandau@xxxxxx> wrote in message
news:2643FA11-7A90-4D22-A14F-F7E928C8E55B@xxxxxx
Quote:

> Thank you for being patient with me. I had to look back on my previous
> code
> version to see what provoked me to go the path I did. Some logic flaw
> which I
> didn't see caused me to switch to my direction.
No worries. That's what community is here for.

Jacques

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Copy-Item : Container cannot be copied onto existing leaf item. PowerShell
Can the copy-item cmdlet be used to copy public folders to C: ? PowerShell
copy-item changing files attributes on network copy failures PowerShell
Copy-Item PowerShell
Copy-Item or Copy-ItemProperty and Remote Registry. PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46