Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Delete Files-n-Folders with specific criteria

$
0
0

Hi there

I'm trying to create a script in powershell which would do the following for me

E:\Publish\A\<timestamp folders like 2014_10_27_04_58_19-.1.4.4.1224.Debug. Each of these publish folders have many files/folders within it>. 

E:\Publish\B\<timestamp folders>

E:\Publish\C\<timestamp folders>

E:\Publish\D\<timestamp folders>

E:\Publish\E\<timestamp folders>

The problem i have is the below script wouldn't allow me to delete everything and retain the last (recent)10 publish folders.

Any idea how i can go about achieving this?

 

$folder = "E:\Publish"

$excludedFolders = "E:\Publish\A", "E:\Publish\B", "E:\Publish\C"

$itemsFromFolder = get-childitem $folder -recurse

$itemsToDelete = New-Object System.Collections.Generic.List[string]

foreach($item in $itemsFromFolder)

{

    $exclude = $false

foreach($exclusion in $excludedFolders)

    {       

    if($item.FullName.ToLower().StartsWith($exclusion.ToLower()))

    {

        $exclude = $true

    }

}

if(!$exclude)

{

    $itemsToDelete.Add($item.FullName)

}

}

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles