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 - Compare-Object and Get the name of object/File?

Reply
 
Old 06-16-2008   #1 (permalink)
akcorr


 
 

Compare-Object and Get the name of object/File?

I want to compare two directories and copy the files that are not in
directory $A to directory $B. My initial try to see what I get back:

$A = Get-ChildItem -path C:\test1
$B = Get-ChildItem -path C:\test2

$C = Compare-Object $A $B

ForEach ($f in $C)
{
Write-Host $f
}

It Spit back this:

@{InputObject=test.txt; SideIndicator=<=}
@{InputObject=test2.txt; SideIndicator=<=}
@{InputObject=test3.zip; SideIndicator=<=}
@{InputObject=test4.txt; SideIndicator=<=}

How would I got about extracting the fullnames for each of those files?
After that I can pass it to a function for work to be done.



My System SpecsSystem Spec
Old 06-16-2008   #2 (permalink)
Marco Shaw [MVP]


 
 

Re: Compare-Object and Get the name of object/File?

akcorr wrote:
Quote:

> I want to compare two directories and copy the files that are not in
> directory $A to directory $B. My initial try to see what I get back:
>
> $A = Get-ChildItem -path C:\test1
> $B = Get-ChildItem -path C:\test2
>
> $C = Compare-Object $A $B
Quote:

> How would I got about extracting the fullnames for each of those files?
> After that I can pass it to a function for work to be done.
I'd skip the foreach loop:

PS > $C|ForEach-Object{$_.InputObject}|ForEach-Object{$_.FullName}
C:\test2\bar.txt
C:\test1\foo.txt
PS >

That could also be more simply:
$C|ForEach-Object{($_.InputObject).FullName}

Hope that helps...

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 06-17-2008   #3 (permalink)
Shay Levi


 
 

Re: Compare-Object and Get the name of object/File?

Hi akcorr,

compare-Object $A $B | select InputObject

Make sure to read Dmitry's post on the -syncWindow parameter:
http://dmitrysotnikov.wordpress.com/...object-gotcha/



---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

a> I want to compare two directories and copy the files that are not in
a> directory $A to directory $B. My initial try to see what I get back:
a>
a> $A = Get-ChildItem -path C:\test1
a> $B = Get-ChildItem -path C:\test2
a> $C = Compare-Object $A $B
a>
a> ForEach ($f in $C)
a> {
a> Write-Host $f
a> }
a> It Spit back this:
a>
a> @{InputObject=test.txt; SideIndicator=<=}
a> @{InputObject=test2.txt; SideIndicator=<=}
a> @{InputObject=test3.zip; SideIndicator=<=}
a> @{InputObject=test4.txt; SideIndicator=<=}
a> How would I got about extracting the fullnames for each of those
a> files? After that I can pass it to a function for work to be done.
a>


My System SpecsSystem Spec
Old 06-17-2008   #4 (permalink)
/\/\o\/\/ [MVP]


 
 

Re: Compare-Object and Get the name of object/File?

a> I want to compare two directories and copy the files that are not in
Quote:

> a> directory $A to directory $B.
Another way to do this from PowerShell that might be interesting :

copy c:\test1\*.* c:\test2 -Exclude (dir c:\test2) -WhatIf

Greetings /\/\o\/\/

"Shay Levi" wrote:
Quote:

> Hi akcorr,
>
> compare-Object $A $B | select InputObject
>
> Make sure to read Dmitry's post on the -syncWindow parameter:
> http://dmitrysotnikov.wordpress.com/...object-gotcha/
>
>
>
> ---
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
> a> I want to compare two directories and copy the files that are not in
> a> directory $A to directory $B. My initial try to see what I get back:
> a>
> a> $A = Get-ChildItem -path C:\test1
> a> $B = Get-ChildItem -path C:\test2
> a> $C = Compare-Object $A $B
> a>
> a> ForEach ($f in $C)
> a> {
> a> Write-Host $f
> a> }
> a> It Spit back this:
> a>
> a> @{InputObject=test.txt; SideIndicator=<=}
> a> @{InputObject=test2.txt; SideIndicator=<=}
> a> @{InputObject=test3.zip; SideIndicator=<=}
> a> @{InputObject=test4.txt; SideIndicator=<=}
> a> How would I got about extracting the fullnames for each of those
> a> files? After that I can pass it to a function for work to be done.
> a>
>
>
>
My System SpecsSystem Spec
Old 06-17-2008   #5 (permalink)
Shay Levi


 
 

Re: Compare-Object and Get the name of object/File?


Definitely!

--
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

a>> I want to compare two directories and copy the files that are not in
a>>
Quote:
Quote:

>> a> directory $A to directory $B.
>>
o> Another way to do this from PowerShell that might be interesting :
o>
o> copy c:\test1\*.* c:\test2 -Exclude (dir c:\test2) -WhatIf
o>
o> Greetings /\/\o\/\/
o>
o> "Shay Levi" wrote:
o>
Quote:
Quote:

>> Hi akcorr,
>>
>> compare-Object $A $B | select InputObject
>>
>> Make sure to read Dmitry's post on the -syncWindow parameter:
>> http://dmitrysotnikov.wordpress.com/...object-gotcha/
>>
>> ---
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> a> I want to compare two directories and copy the files that are not
>> in
>> a> directory $A to directory $B. My initial try to see what I get
>> back:
>> a>
>> a> $A = Get-ChildItem -path C:\test1
>> a> $B = Get-ChildItem -path C:\test2
>> a> $C = Compare-Object $A $B
>> a>
>> a> ForEach ($f in $C)
>> a> {
>> a> Write-Host $f
>> a> }
>> a> It Spit back this:
>> a>
>> a> @{InputObject=test.txt; SideIndicator=<=}
>> a> @{InputObject=test2.txt; SideIndicator=<=}
>> a> @{InputObject=test3.zip; SideIndicator=<=}
>> a> @{InputObject=test4.txt; SideIndicator=<=}
>> a> How would I got about extracting the fullnames for each of those
>> a> files? After that I can pass it to a function for work to be
>> done.
>> a>

My System SpecsSystem Spec
Old 06-19-2008   #6 (permalink)
Chris Warwick


 
 

Re: Compare-Object and Get the name of object/File?

On Tue, 17 Jun 2008 06:24:01 -0700, /\/\o\/\/ [MVP]
<oMVP@xxxxxx> wrote:
Quote:

>a> I want to compare two directories and copy the files that are not in
Quote:

>> a> directory $A to directory $B.
>
>Another way to do this from PowerShell that might be interesting :
>
>copy c:\test1\*.* c:\test2 -Exclude (dir c:\test2) -WhatIf
>
>Greetings /\/\o\/\/
>
To list the files that would be copied:

robocopy c:\test1 c:\test2 /l

Remove /l to actually copy the files....

:-)




My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
compare-object PowerShell
Formating with Compare-Object PowerShell
compare object PowerShell
Testing object arrays using Compare-Object and -contains PowerShell
Adding canonical aliases for Compare-Object, Measure-Object, New-Object 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