Execute function for each list item in powershell

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

I got to:

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

But I can’t figure out how to pass each name to [System.IO.Path] :: GetFileNameWithoutExtension

How can i do this?

+3
source share
4 answers

What you are looking for is a filter for each (%) (not quite sure if this is a filter or a cmdlet, but it has the same usage syntax).

Try to execute

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

EDIT Update

"select-object Name" "%{ $_.Name}". Name . $_.Name .

+6

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

+4

% {$ _. foo} , , Get-PropertyValue (alias: gpv) PSCX.

geeky: http://richardberg.net/blog/?p=55

+2

, PowerShell v2 RTMd, BaseName:

dir -recurse -filter *.mp3 | BaseName |

+2

Source: https://habr.com/ru/post/1714431/


All Articles