Windows Vista Forums

Creating a list of files that have been deleted from or added to a directory

  1. #1


    hrh1818 Guest

    Creating a list of files that have been deleted from or added to a directory

    In Powershell if I have the following two arrays:
    $a = 'file1', 'file2', 'file3'
    $b = 'file2', 'file3', 'file4'
    The operation $a - $b produces a method not implemented error.

    Whereas in Ruby if I have the following two arrays:
    a = ['file1', 'file2', 'file3']
    b = ['file2', 'file3', 'file4']
    The operation a - b produces the output ["file1"]
    and the operation b - a produces the output ["file4"]

    This use of the subtraction operator in Ruby is very useful when
    creating a list of files that have been deleted or added to a
    directory. What method in Powershell will produce the same results as
    a - b does with arrays in Ruby?

    Howard


      My System SpecsSystem Spec

  2. #2


    Brandon Shell [MVP] Guest

    Re: Creating a list of files that have been deleted from or added to a directory

    Perhaps Compare-Object is what your looking for?

    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    h> In Powershell if I have the following two arrays:
    h> $a = 'file1', 'file2', 'file3'
    h> $b = 'file2', 'file3', 'file4'
    h> The operation $a - $b produces a method not implemented error.
    h> Whereas in Ruby if I have the following two arrays:
    h> a = ['file1', 'file2', 'file3']
    h> b = ['file2', 'file3', 'file4']
    h> The operation a - b produces the output ["file1"]
    h> and the operation b - a produces the output ["file4"]
    h> This use of the subtraction operator in Ruby is very useful when
    h> creating a list of files that have been deleted or added to a
    h> directory. What method in Powershell will produce the same results
    h> as a - b does with arrays in Ruby?
    h>
    h> Howard
    h>



      My System SpecsSystem Spec

  3. #3


    Kuma Guest

    Re: Creating a list of files that have been deleted from or added to a directory

    On Oct 12, 12:17 am, hrh1818 <hr...@xxxxxx> wrote:

    > In Powershell if I have the following two arrays:
    > $a = 'file1', 'file2', 'file3'
    > $b = 'file2', 'file3', 'file4'
    > The operation $a - $b produces a method not implemented error.
    >
    > Whereas in Ruby if I have the following two arrays:
    > a = ['file1', 'file2', 'file3']
    > b = ['file2', 'file3', 'file4']
    > The operation a - b produces the output ["file1"]
    > and the operation b - a produces the output ["file4"]
    >
    > This use of the subtraction operator in Ruby is very useful when
    > creating a list of files that have been deleted or added to a
    > directory. What method in Powershell will produce the same results as
    > a - b does with arrays in Ruby?
    >
    > Howard

    Howard,
    I've done it in the past with foreach loops.

    [6] $a = 'file1', 'file2', 'file3'
    [7] $b = 'file2', 'file3', 'file4'
    [8] foreach ($i in $a){If($b -notcontains $i){$i}}
    file1
    [9] foreach ($i in $b){If($a -notcontains $i){$i}}
    file4

    There's probably a better way to do it, but it doesn't come to mind at
    the moment.

    Kuma


      My System SpecsSystem Spec

  4. #4


    Shay Levi Guest

    Re: Creating a list of files that have been deleted from or added to a directory


    Try the Compare-Object cmdlet

    Compare-Object $a $b

    PS C:\Scripts> $a = 'file1', 'file2', 'file3'
    PS C:\Scripts> $b = 'file2', 'file3', 'file4'
    PS C:\Scripts> Compare-Object $a $b


    InputObject SideIndicator
    ----------- -------------
    file4 =>
    file1 <=


    For further help type

    help Compare-Object -full


    Shay
    http://scriptolog.blogspot.com



    > In Powershell if I have the following two arrays:
    > $a = 'file1', 'file2', 'file3'
    > $b = 'file2', 'file3', 'file4'
    > The operation $a - $b produces a method not implemented error.
    > Whereas in Ruby if I have the following two arrays:
    > a = ['file1', 'file2', 'file3']
    > b = ['file2', 'file3', 'file4']
    > The operation a - b produces the output ["file1"]
    > and the operation b - a produces the output ["file4"]
    > This use of the subtraction operator in Ruby is very useful when
    > creating a list of files that have been deleted or added to a
    > directory. What method in Powershell will produce the same results as
    > a - b does with arrays in Ruby?
    >
    > Howard
    >


      My System SpecsSystem Spec

  5. #5


    Kiron Guest

    Re: Creating a list of files that have been deleted from or added to a directory

    Use where-object and -notContains to compare the collections:

    $a = 'file1', 'file2', 'file3'
    $b = 'file2', 'file3', 'file4'

    $a | where-object {$b -notContains $_}
    $b | where-object {$a -notContains $_}

    # create a function
    function RubySubtraction ([array]$a, [array]$b)
    {
    $a | where-object {$b -notContains $_}
    }

    # set its alias
    set-alias ruby`- RubySubtraction

    # call the function
    RubySubtraction $a $b

    # call the functi9n through its alias
    ruby- $b $a

    --
    Kiron

      My System SpecsSystem Spec

  6. #6


    hrh1818 Guest

    Re: Creating a list of files that have been deleted from or added to a directory

    On Oct 11, 10:30 am, Shay Levi <n...@xxxxxx> wrote:

    > Try the Compare-Object cmdlet
    >
    > Compare-Object $a $b
    >
    > PS C:\Scripts> $a = 'file1', 'file2', 'file3'
    > PS C:\Scripts> $b = 'file2', 'file3', 'file4'
    > PS C:\Scripts> Compare-Object $a $b
    >
    > InputObject SideIndicator
    > ----------- -------------
    > file4 =>
    > file1 <=
    >
    > For further help type
    >
    > help Compare-Object -full
    >
    > Shayhttp://scriptolog.blogspot.com
    >
    Shay, Thank you for your reply.
    Compare-object works perfectly with directory listings. But it
    produces a different output than Ruby's Array difference when there
    are duplicate items in an array. PowerShell produces the following
    output with duplicate items.

    $a = 1, 1, 2, 2, 3, 3, 4, 5
    $b = 1, 2, 4
    compare-object $a $b

    InputObject SideIndicator
    ----------- -------------
    1 <=
    2 <=
    3 <=
    3 <=
    5 <=

    Where as Ruby produces the following output with duplicate items.

    irb(main):001:0> a = [1, 1, 2, 2, 3, 3, 4, 5]
    => [1, 1, 2, 2, 3, 3, 4, 5]
    irb(main):002:0> b = [1, 2, 4]
    => [1, 2, 4]
    irb(main):003:0> a - b
    => [3, 3, 5]

    Hence I conclude Powershell"s compare-object is not an exact
    equivalent for Ruby's array Difference.

    Howard





      My System SpecsSystem Spec


Creating a list of files that have been deleted from or added to a directory
Similar Threads
Thread Forum
where can I get a list of files that can be deleted Server General
How to list all files under a directory? Vista General
rename all files from a directory to a list of files ... PowerShell
creating files in the system directory Vista file management
re: creating files in the system directory Vista file management