Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Group unique filenames by expression?

Posted on 2010-11-10
2
Medium Priority
?
818 Views
Last Modified: 2012-08-13
I have a directory full of unique names, all in the same format - Scriptname_datecodestuff.txt
How can I group these based on 'scriptname' ?
What I need to accomplish is I only want to have 10 'versions' of a file in this directory, so if their are more than 10, I need to delete the oldest of that group

Example files in the dir
localhost_11.02.2010-0645.txt
localhost_11.02.2010-0655.txt
localhost_11.02.2010-0705.txt
otherhost_11.02.2010-0645.txt
otherhost_11.02.2010-0650.txt
otherhost_11.02.2010-0650.txt

With those listtings, the 2 groups would be localhost with a count of 3, and otherhost with a count of 3.

I have something semi working, but its not elogant, and suffers some bad performance..
$Group =$File.Name.SubString(0,(($File.Name.Length)-20))
ElseIf (($Directory | Where {$_.Name -Match $Group} | %{$_.Name.SubString(0,(($_.Name.Length)-20))} | Group | %{$_.count}) -gt 10) {
		$Directory | Where {$_.Name -Match $Group} | Sort -Property LastWriteTime | Select -First 1 | Remove-Item

Open in new window

0
Comment
Question by:rwskas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Author Comment

by:rwskas
ID: 34103817
Sorry, that attached code was taken out of the entire script to just show the pieces that are relevant, the 2 lines dont flow together.
$file is a gci object
$directory = gci c:\temp
0
 
LVL 13

Accepted Solution

by:
soostibi earned 2000 total points
ID: 34107118
Try this:

Get-ChildItem C:\temp | foreach-object {
    Add-Member -InputObject $_ -MemberType noteproperty -Name Groupname -Value $(if($_.name -match "^[^_]*"){$matches[0]}else{$null}) -PassThru} | ?{$_.groupname} |
        Group-Object -Property groupname | ?{$_.count -gt 10} | foreach-object {
            $_.group | Sort-Object -Property lastwritetime | Select-Object -First 1 | Remove-Item
        }

Open in new window

0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A brief introduction to what I consider to be the best editor for PowerShell.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question