• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

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

H

hrh1818

#1
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
 
B

Brandon Shell [MVP]

#2
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>
 
K
#3
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
 
S

Shay Levi

#4
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
>
 
K

Kiron

#5
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
 
H

hrh1818

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