Windows Vista Forums

Compare-Object on directory listing
  1. #1


    uj Guest

    Compare-Object on directory listing

    Hello!

    I have some problem with Compare-Object. My task is to get difference
    between two directory snapshots made at different times. First
    snapshot is taken like this:

    > ls -recurse d:\dir | export-clixml dir-20100129.xml
    Then, later, I get second snapshot and load first:

    > $b = (ls -recurse d:\dir)
    > $a = (import-clixml dir-20100129.xml)
    Next, I'm trying to compare with Compare-Object, like that:

    > diff $a $b
    What I get is in some places files that were added to $b since $a, but
    in some -- files that were in both snapshots, and some files, that
    were added to $b, are not given in Compare-Object output. Puzzling,
    but $b.count - $a.count is EXACTLY the same as (diff $a $b).count. Why
    is that?

    Ok, Compare-Object has -property param. I try to use that:

    > diff -property fullname $a $b
    And I get the whole mess of differences: it shows me ALL the files.
    For example, say $a contains:

    A\1.txt
    A\2.txt
    A\3.txt

    And $b contains:

    X\2.mp3
    X\3.mp3
    X\4.mp3
    A\1.txt
    A\2.txt
    A\3.txt

    diff output is something like that:

    X\2.mp3 =>
    A\1.txt <=
    X\3.mp3 =>
    A\2.txt <=
    X\4.mp3 =>
    A\3.txt <=
    A\1.txt =>
    A\2.txt =>
    A\3.txt =>



    Weird. I think I don't understand something crucial about Compare-
    Object usage, and manuals are scarce... Please, help me to get the
    DIFFERENCE between two directory snapshots. Thanks in advance

      My System SpecsSystem Spec

  2. #2


    stej Guest

    Re: Compare-Object on directory listing

    IMO it is cause by the Export/Import-CliXml, because this works as
    expected:

    $a = "A\1.txt
    A\2.txt
    A\3.txt" -split "`n"

    $b = "X\2.mp3
    X\3.mp3
    X\4.mp3
    A\1.txt
    A\2.txt
    A\3.txt" -split "`n"

    Compare-Object $a $b
    (i use "`n" cause "`r`n" doesn't work in posh console)

    I'll check the problem later. Meantime you coul export-clixml the $b
    collection, import it back and compare the $a and $b.
    Both are cli-imported so then it could work

    On Jan 29, 8:07*pm, uj <andrey.balag...@newsgroup> wrote:

    > Hello!
    >
    > I have some problem with Compare-Object. My task is to get difference
    > between two directory snapshots made at different times. First
    > snapshot is taken like this:
    >

    > > ls -recurse d:\dir | export-clixml dir-20100129.xml
    >
    > Then, later, I get second snapshot and load first:
    >

    > > $b = (ls -recurse d:\dir)
    > > $a = (import-clixml dir-20100129.xml)
    >
    > Next, I'm trying to compare with Compare-Object, like that:
    >

    > > diff $a $b
    >
    > What I get is in some places files that were added to $b since $a, but
    > in some -- files that were in both snapshots, and some files, that
    > were added to $b, are not given in Compare-Object output. Puzzling,
    > but $b.count - $a.count is EXACTLY the same as (diff $a $b).count. Why
    > is that?
    >
    > Ok, Compare-Object has -property param. I try to use that:
    >

    > > diff -property fullname $a $b
    >
    > And I get the whole mess of differences: it shows me ALL the files.
    > For example, say $a contains:
    >
    > A\1.txt
    > A\2.txt
    > A\3.txt
    >
    > And $b contains:
    >
    > X\2.mp3
    > X\3.mp3
    > X\4.mp3
    > A\1.txt
    > A\2.txt
    > A\3.txt
    >
    > diff output is something like that:
    >
    > X\2.mp3 =>
    > A\1.txt <=
    > X\3.mp3 =>
    > A\2.txt <=
    > X\4.mp3 =>
    > A\3.txt <=
    > A\1.txt =>
    > A\2.txt =>
    > A\3.txt =>
    >
    > Weird. I think I don't understand something crucial about Compare-
    > Object usage, and manuals are scarce... Please, help me to get the
    > DIFFERENCE between two directory snapshots. Thanks in advance

      My System SpecsSystem Spec

  3. #3


    stej Guest

    Re: Compare-Object on directory listing

    When the $a is exported and imported as clixml, the types change.
    System.IO.FileInfo objects
    change to Deserialized.System.IO.FileInfo. I don't know the internals,
    but I would bet this is the problem.

    If you will compare like this:
    $a = ls -recurse d:\dir | select -exp FullName | export-clixml
    dir-20100129.xml
    $a = import-clixml dir-20100129.xml
    ....
    $b = ls -recurse d:\dir | select -exp FullName

    there should be no problem. String is serialized and deserialized as
    string.

    On Jan 29, 8:07*pm, uj <andrey.balag...@newsgroup> wrote:

    > Hello!
    >
    > I have some problem with Compare-Object. My task is to get difference
    > between two directory snapshots made at different times. First
    > snapshot is taken like this:
    >

    > > ls -recurse d:\dir | export-clixml dir-20100129.xml
    >
    > Then, later, I get second snapshot and load first:
    >

    > > $b = (ls -recurse d:\dir)
    > > $a = (import-clixml dir-20100129.xml)
    >
    > Next, I'm trying to compare with Compare-Object, like that:
    >

    > > diff $a $b
    >
    > What I get is in some places files that were added to $b since $a, but
    > in some -- files that were in both snapshots, and some files, that
    > were added to $b, are not given in Compare-Object output. Puzzling,
    > but $b.count - $a.count is EXACTLY the same as (diff $a $b).count. Why
    > is that?
    >
    > Ok, Compare-Object has -property param. I try to use that:
    >

    > > diff -property fullname $a $b
    >
    > And I get the whole mess of differences: it shows me ALL the files.
    > For example, say $a contains:
    >
    > A\1.txt
    > A\2.txt
    > A\3.txt
    >
    > And $b contains:
    >
    > X\2.mp3
    > X\3.mp3
    > X\4.mp3
    > A\1.txt
    > A\2.txt
    > A\3.txt
    >
    > diff output is something like that:
    >
    > X\2.mp3 =>
    > A\1.txt <=
    > X\3.mp3 =>
    > A\2.txt <=
    > X\4.mp3 =>
    > A\3.txt <=
    > A\1.txt =>
    > A\2.txt =>
    > A\3.txt =>
    >
    > Weird. I think I don't understand something crucial about Compare-
    > Object usage, and manuals are scarce... Please, help me to get the
    > DIFFERENCE between two directory snapshots. Thanks in advance

      My System SpecsSystem Spec

  4. #4


    uj Guest

    Re: Compare-Object on directory listing

    On Jan 29, 9:23*pm, stej <cerna.ze...@newsgroup> wrote:

    > If you will compare like this:
    > $a = ls -recurse d:\dir | select -exp FullName | export-clixml
    > dir-20100129.xml
    Actually, both snapshots are imported with Import-Clixml. But I tried
    to dump them just as strings with:

    > $a | % { $_.fullname } | out-file a.txt
    > $b | % { $_.fullname } | out-file b.txt
    And, once again, Compare-Object gives me a whole lot of "differences":

    > diff (get-content a.txt) (get-content b.txt)
    I've event tried it with UNIX diff, and it actually gives reasonable
    output.

    --
    Best Regards, Andrey Balaguta

      My System SpecsSystem Spec

  5. #5


    stej Guest

    Re: Compare-Object on directory listing

    For me this works as expected:

    gci D:\temp\test\ -rec | select -exp fullname | Out-File d:\temp
    \outfile1.txt
    #...change something
    gci D:\temp\test\ -rec | select -exp fullname | Out-File d:\temp
    \outfile2.txt
    Compare-Object (gc D:\temp\outfile1.txt) (gc D:\temp\outfile2.txt)

    Could je publish somewhere the files to examine them?

    On Jan 29, 8:39*pm, uj <andrey.balag...@newsgroup> wrote:

    > On Jan 29, 9:23*pm, stej <cerna.ze...@newsgroup> wrote:
    >

    > > If you will compare like this:
    > > $a = ls -recurse d:\dir | select -exp FullName | export-clixml
    > > dir-20100129.xml
    >
    > Actually, both snapshots are imported with Import-Clixml. But I tried
    > to dump them just as strings with:
    >

    > > $a | % { $_.fullname } | out-file a.txt
    > > $b | % { $_.fullname } | out-file b.txt
    >
    > And, once again, Compare-Object gives me a whole lot of "differences":
    >

    > > *diff (get-content a.txt) (get-content b.txt)
    >
    > I've event tried it with UNIX diff, and it actually gives reasonable
    > output.
    >
    > --
    > Best Regards, Andrey Balaguta

      My System SpecsSystem Spec

  6. #6


    uj Guest

    Re: Compare-Object on directory listing

    On Jan 29, 9:55*pm, stej <cerna.ze...@newsgroup> wrote:

    > Could je publish somewhere the files to examine them?

    Here's archive with excerpts of both snapshot (top 100-something
    lines, a.txt and b.txt), output of compare-object, and output of UNIX
    diff (unified). All files are UTF-8:

    http://91.204.198.27/compare-object-problem.zip

    --
    Best Regards, Andrey Balaguta

      My System SpecsSystem Spec

  7. #7


    uj Guest

    Re: Compare-Object on directory listing

    On Jan 30, 10:35*am, uj <andrey.balag...@newsgroup> wrote:

    >
    > Here's archive [...]
    >
    > http://91.204.198.27/compare-object-problem.zip
    >
    Just a follow-up. If that link is unavailable, please use the
    following link: http://dl.dropbox.com/u/2873752/comp...ct-problem.zip

    --
    Best Regards, Andrey Balaguta

      My System SpecsSystem Spec

  8. #8


    stej Guest

    Re: Compare-Object on directory listing

    I downloaded your files and tried to compare it. Here is the result:

    [3]: Compare-Object (gc D:\temp\posh-compare-object-problem\a.txt) (gc
    D:\temp\posh-compare-object-problem\b.txt)

    InputObject
    SideIndicator
    -----------
    -------------
    D:\music
    \5'nizza
    =>
    D:\music
    \Playlists
    =>
    D:\music
    \RĂşn
    =>
    D:\music
    \SunSay
    =>
    D:\music\КрематоŃ
    €Đ¸Đą =>
    D:\music\5'nizza\2002 -
    Unplugged =>
    D:\music\5'nizza\2002 - Unplugged\01 - СолдаŃ
    ‚.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\02 - ПятниŃ*Đ
    °.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\03 - ЯмайкĐ
    °.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\04 - СюрнĐ
    °ŃŹ.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\05 - СĐ
    °Đ˝.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\06 - ĐŻ с тоĐ
    ±ĐľĐą.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\07 - Ты кидĐ
    °Đ».mp3 =>
    D:\music\5'nizza\2002 - Unplugged\08 - ĐŻ не Ń
    ‚ой.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\09 - СтрелĐ
    °.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\10 - ВодĐ
    °.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\11 - Я тебя
    вы.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\12 - ВеснĐ
    °.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\13 -
    Bigbadboom.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\14 - НевĐ
    °.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\15 - СвободĐ
    °.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\16 - Ушедшим слишком Ń
    €Đ°Đ˝Đľ.mp3 =>
    D:\music\5'nizza\2002 - Unplugged\17 - ДжĐ
    °ĐĽĐ¸Đ˝.mp3 =>
    D:\music\5'nizza\2002 - Unplugged
    \folder.jpg =>
    D:\music\Johnny
    Cash
    <=
    [4]:

    It looks ok. I have no idea what is going on there on your computer.
    What versions ov PowerShell do you use? V2?

    On Jan 30, 9:35*am, uj <andrey.balag...@newsgroup> wrote:

    > On Jan 29, 9:55*pm, stej <cerna.ze...@newsgroup> wrote:
    >

    > > Could je publish somewhere the files to examine them?
    >
    > Here's archive with excerpts of both snapshot (top 100-something
    > lines, a.txt and b.txt), output of compare-object, and output of UNIX
    > diff (unified). All files are UTF-8:
    >
    > http://91.204.198.27/compare-object-problem.zip
    >
    > --
    > Best Regards, Andrey Balaguta

      My System SpecsSystem Spec

  9. #9



    Junior Member
    Join Date : Mar 2008
    Posts : 11
    XP
    Local Time: 04:25 PM
    france

     

    Re: Compare-Object on directory listing

      My System SpecsSystem Spec

  10. #10


    uj Guest

    Re: Compare-Object on directory listing

    On Jan 30, 10:47*pm, stej <cerna.ze...@newsgroup> wrote:

    > It looks ok. I have no idea what is going on there on your computer.
    > What versions ov PowerShell do you use? V2?
    Well, that's weird. Yes, I'm using PowerShell V2 CTP. I don't know how
    our versions could differ, I have PSCX on my machine installed, but
    I've tried running powershell -NoProfile (so I think it pretty much
    'vanilla') and I still get the same results.

    --
    Best Regards, Andrey Balaguta

      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
Compare-Object on directory listing problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
compare-object Carmen A. Puccio PowerShell 0 21 May 2009
compare object IT Staff PowerShell 0 24 Oct 2008
Compare-Object and Get the name of object/File? akcorr PowerShell 5 19 Jun 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