Question

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

Was it helpful?

Solution

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 }

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top