tags:

views:

133

answers:

4

I have a directory full of file pairs. Each pair of files have the same name with the extensions of mp3 and cdg (karaoke files!). I would like to use powershell to get the list of all distinct file names with no extensions

I've gotten as far as:

dir -recurse -filter "*.mp3" | select-object Name | sort

But I can't quite figure out how to pass each Name to [System.IO.Path]::GetFileNameWithoutExtension

how would I do this?

+3  A: 

What you're looking for is the for-each (%) filter (not precisely sure if it's a filter or a cmdlet but it has the same usage syntax).

Try the following

dir -recurse -filter "*.mp3" | 
  %{ $_.Name } |
  %{ [IO::Path]::GetFileNameWithoutExtension($_) } |
  sort

EDIT Update

I changed my answer from "select-object Name" to "%{ $_.Name}". The former essentially takes the Name property off of the pipeline value and creates a new object with a single property of the specified name with the value on the original object. The latter will process every value in the pipeline and pass the result of executing $_.Name down the pipeline.

JaredPar
That works but a) it gives me something like: @{Name=Mr Bungle - Backstrokin' - How do I get rid of the @{Name= part? and b) what the heck did I just do?
George Mauer
@George, I updated my answer to get rid of the @Name part. I'll add a response as to why that happens
JaredPar
Awesome, thanks
George Mauer
foreach-object is a function. You can verify this with gcm or simply by observing that % { "begin" } { "processing $_" } { "end" } works as expected.
Richard Berg
ForEach-Object is a cmdlet here.
Joey
Richard Berg
+2  A: 

If you hate typing %{$_.foo} all the time like I do, try Get-PropertyValue (alias: gpv) from PSCX.

More musings here for the suitably geeky: http://richardberg.net/blog/?p=55

Richard Berg
+4  A: 

dir -recurse -filter ".mp3"| select @{name='Name';Expression={[System.IO.Path]::GetFileNameWithoutExtension($.Name)}} | sort

Chad Miller
+1. Many people forget select's "hashtable syntax." Too bad it's so ugly. Would be much nicer to write select @{ Name = {some-expression}, Path = {some-otherexpression} }
Richard Berg
Agreed Richard.
JasonMArcher
+2  A: 

Now that PowerShell v2 is RTMd, you can select the BaseName member:

dir -recurse -filter *.mp3 | select BaseName | sort

Shay Levy