1.    11 Oct 2007 #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.    11 Oct 2007 #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.    11 Oct 2007 #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.    11 Oct 2007 #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.    11 Oct 2007 #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.    11 Oct 2007 #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


Similar Threads
Thread Forum
vbs script to list files in a directory and email results
Hi. Been trying to write something with out any luck. Im looking for a script that will look in a given directory (i.e. c:\mydata) and e-mail...
VB Script
where can I get a list of files that can be deleted
Using Windows 2003 Enterprise. I want to delete Log files and other files to free up some space. Where can I get a list of what can be deleted?
Server General
How to list all files under a directory?
Ok when I was using Windows XP and I wanted to list all files in a folder and sub folders I would just do a search with the search field blank on...
Vista General
Search returns a list of deleted files
When I perform a search for a text string in a directory (and all it's files and sub-directories), it returns a list of files that were deleted fom...
Vista file management
rename all files from a directory to a list of files ...
hi, what is a script to rename files like track 01a.mp3 track 02a.mp3 track 03a.mp3 track 04a.mp3 ....
PowerShell
creating files in the system directory
Is it possible for a user (the administrator) to delete or overwrite files in the \windows and \windows\system32 directories? I want to copy...
Vista file management
re: creating files in the system directory
I don't understand this statement - if an old application supplies its help in a .hlp file, why do you think there will be a .chm file shipped along...
Vista file management

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • PC Help Forum
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 14:02.
    .