PowerShellTricks

Finding Files In Directories

A Foreach-Object method of finding the first Ogg Vorbis file in each subdirectory

First, the list of directories to feed the Foreach:

Get-ChildItem -Path . -Recurse -Directory

Then, getting just that first Ogg. (All of this so I can make a file to then feed to an 'ogginfo' loop in the bash shell to get Vendor details, UGH.)

Foreach-Object { Get-ChildItem -Filter '*.ogg' | Select-Object -First 1 }

...maybe. Nope, that didn't fly.

Group-Object Directory | ForEach {$_.group | Select -First 1}

Ugh this is a mess. And it doesn't work. Back to bash I guess.

A stupidly simple way to find what I actually needed, which is "show me directories containing OGG files."

Get-ChildItem X:\Path\To\Library\*.ogg -Recurse | Select Directory -Unique

WELL THAT WAS EASIER.

I had to use the Bash method to find the Ogg files to check, NO THANK YOU POWERSHELL, but Bash didn't like the "while read" loop AT ALL.

Get-Content E:\Temp\OggDirsPC.txt | ForEach-Object { C:\Apps\VorbisTools\ogginfo.exe "$_" | Select-String -Pattern Vendor }

Now that does most of the job, but it'd be nice to slice off the initial "Vendor: " from the results... but Perfect is the enemy of Good, we can use search/replace in Notepad++ for this, let's move along shall we?

Except it turns out NOT to do the job. It fails to process any file with double-byte filename characters. Faaaantastic. Next?