![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| 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 Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| 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 Specs![]() |
| | #5 (permalink) |
| 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 Specs![]() |
| | #6 (permalink) |
| 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 > 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 Specs![]() |
![]() |
| 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 | |||