Powershell: Dir /s - Remove Repeated Folders

1

Situation:

In Powershell, I'm using:

$folders = dir -s  C:\Users\Name\Downloads\*.ttf | Select Directory
foreach ($folder in $folders)
{
    echo $folder
}

To display folders whose contents contain certain file extentions (in this instance .ttf files).

Because the folders have more than one file of type .ttf, duplicates appear in the final list. This is not useful for me because I'd like to move the directories listed to somewhere else.

My Question:

How can I remove these duplicates from the echoed list?

Synthetic Ascension

Posted 2015-07-11T21:13:39.307

Reputation: 23

Answers

1

Ok I believe I've sorted it out.

I used the -Unique switch on Select to filter out repeat results.

Giving the final code of:

$folders = dir -s C:\Users\Name\Downloads\*.ttf | Select Directory -Unique
foreach ($folder in $folders)
{
    echo $folder
}

A simple solution, but I hadn't used PowerShell to script before so I wasn't aware of this particular feature :)

Synthetic Ascension

Posted 2015-07-11T21:13:39.307

Reputation: 23

0

You can use Contains to check as you load each item into a list, or `NotContains'

You can try this to get a grasp of contains:

$room = @("desk","Lamp","chair","pens","Paper")
$room -Contains "Lamp"
$room -notContains "Whale Bladder"

An easy (off the top of my head, and not tested) approximation for what you want would look something like:

$FoundFiles = @()
$FolderList | Foreach-Object {
   Get-ChildItems $_ | foreach-Object {if ($FoundFiles -NotContains  $_.Name){
    $FoundFiles += $_
      }
    }
}

Austin T French

Posted 2015-07-11T21:13:39.307

Reputation: 9 766