sort substrings of filenames, giving unique values


sort substrings of filenames, giving unique values



FWIW - I had fun "developing" this simple routine which will list the unique artists among a listing of several hundred files in a single directory of MP3 files



Based on pattern "artist - title.mp3"


Get-ChildItem | ForEach-Object {($_.basename -split ("-"))[0]} | Select-Object -unique



What I would like to do now is have a count next to each unique name (i.e. Duke Ellington - 4 Doris Day - 6



Not sure how to do this but it is probably very simple. Was tryng to use the group statement, but no luck



Bill Clark, Windham,VT





I'd take a look at hashtables.. and maintain counts in them. kevinmarquette.github.io/…
– JGFMK
Jul 2 at 17:44




1 Answer
1



Assuming all your files follow that pattern:


Get-ChildItem -File |
Group-Object -Property {$_.BaseName.Split('-')[0]}



This will automatically group all the strings returned from the previous item in the pipeline.



Then you can do additional fun things with this:


Get-ChildItem -File |
Group-Object -Property {$_.BaseName.Split('-')[0]} |
ForEach-Object {
$dir = "$([IO.Path]::GetDirectoryName($_.Group[0].FullName))$($_.Name)"
if (-not (Test-Path -Path $dir -PathType Container)) {
New-Item -Path $dir -ItemType Directory
}

foreach ($file in $_.Group) {
$file | Copy-Item -Destination $dir -Force
}
}



And now all your songs are sorted by artist in folders.





Once more - the answers are fabulous, and your solution does exactly what I was looking for ... learning so much, and thanks !
– kappclark
Jul 2 at 21:04






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages