rwskas
asked on
Group unique filenames by expression?
I have a directory full of unique names, all in the same format - Scriptname_datecodestuff.t xt
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..
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.
localhost_11.02.2010-0655.
localhost_11.02.2010-0705.
otherhost_11.02.2010-0645.
otherhost_11.02.2010-0650.
otherhost_11.02.2010-0650.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
$file is a gci object
$directory = gci c:\temp