0

I need to write a powershell script, which list file names that contains the same letters, only difference is the sort of the letters.

My first oppinion is to sort the letters in alphabet, and if it fit, then they match, but i need some help for do that

Get-ChildItem $path | foreach  {$i=1}`
{  
        $asd=$_ | sort-object
        Get-ChildItem $path | foreach  {$i=1}`
        {  
            $wasd=$_ | sort-object              
            if($asd -eq $wasd)
            {
                Write-Host $_
            }

        }
}

This files match for my criteria: asd.txt, dsa.txt, because contains same letters

user3063349
  • 96
  • 1
  • 10
  • `code` Get-ChildItem $path | foreach {$i=1}` { $asd=$_ | sort Get-ChildItem $path | foreach {$i=1}` { $wasd=$_ | sort Write-Host $wasd " " $asd if($asd -eq $wasd) { Write-Host $_ } } } `code` This way of sort didnt work – user3063349 Dec 12 '13 at 16:41
  • Get-ChildItem $path | foreach {$i=1}` { $asd=$_ | sort Get-ChildItem $path | foreach {$i=1}` { $wasd=$_ | sort Write-Host $wasd " " $asd if($asd -eq $wasd) { Write-Host $_ } } } – user3063349 Dec 12 '13 at 16:41
  • 1
    Please give examples of the file names that would fit your criteria. – Benjamin Hubbard Dec 12 '13 at 16:50
  • 1
    Put your code in your post, not in a comment. It won't be formatted properly in a comment. – alroc Dec 12 '13 at 16:52

2 Answers2

1

I think this is doing what you want.

function get-Stringcharacters {
param($string)
  [char[]]$string | sort-object
}

dir $path | group-object @{E={get-Stringcharacters $_.Name}} | 
          where-object {$_.Count -gt 1} | 
          select-object -ExpandProperty Group |
          foreach { write-host $_.Name }
Mike Shepard
  • 17,466
  • 6
  • 51
  • 69
0

gci | % { ($.BaseName.ToString().toCharArray() | Sort) -join ''} | Group | ? { $.Count -gt 1 } | Select Name

KevinD
  • 3,023
  • 2
  • 22
  • 26