Windows Vista Forums

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


    akcorr Guest

    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

  2. #2


    Marco Shaw [MVP] Guest

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

    akcorr wrote:

    > 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

    > 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

  3. #3


    Shay Levi Guest

    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

  4. #4


    /\/\o\/\/ [MVP] Guest

    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

    > 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:

    > 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

  5. #5


    Shay Levi Guest

    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>>

    >> 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>

    >> 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

  6. #6


    Chris Warwick Guest

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

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

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

    >> 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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
compare-object Carmen A. Puccio PowerShell 0 21 May 2009
Formating with Compare-Object Matt PowerShell 2 27 Oct 2008
compare object IT Staff PowerShell 0 24 Oct 2008
Testing object arrays using Compare-Object and -contains Alex K. Angelopoulos [MVP] PowerShell 2 31 Aug 2006
Adding canonical aliases for Compare-Object, Measure-Object, New-Object Alex K. Angelopoulos [MVP] PowerShell 2 26 May 2006