Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

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

Reply
 
Old 10-11-2007   #1 (permalink)
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
Old 10-11-2007   #2 (permalink)
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
Old 10-11-2007   #3 (permalink)
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:
Quote:

> 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
Old 10-11-2007   #4 (permalink)
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


Quote:

> 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
Old 10-11-2007   #5 (permalink)
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
Old 10-11-2007   #6 (permalink)
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:
Quote:

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

Thread Tools


Similar Threads
Thread Forum
How to list all files under a directory? Vista General
Search returns a list of deleted files Vista file management
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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46