Batch Applying MetaFLAC ReplayGain Tags To Recursive Album Folders

I’ve been ignoring PowerShell for a very long time, even though I know it is the future of scripting.  The structure of the language is very foreign, with all of the pipe options.  But I muddled through the examples I found online and came up with my first usable PowerShell script.

The purpose of the script is to iterate recursively through a folder structure and generate a command statement using all the files in each folder.  In this case, MetaFLAC is being called with arguments to apply ReplayGain tags to all the files in a folder.  To do this effectively, you have to pass all the file names in the folder on one command line.  This is so the overall album sound level can be calculated.

Without further introduction, here is the script:

<#
    This script applies album-level and file-level
    ReplayGain tags to FLAC album folders recursively

    Example usage from PS command line:
    .\ReplayGain.ps1 -root:"c:\music\FLAC"

    MetaFLAC.exe must be in same folder or in environment PATH
#>

Param(
    #Fully-qualified path to top-level folder.
    [string]$root
    )

Function CallMetaFLAC($d){
    Write-Host "Processing" ($d | Get-ChildItem -Filter "*.flac").length "files in" $d
    
    if (($d | Get-ChildItem -Filter "*.flac").length -gt 0){
        $args="–add-replay-gain "

        foreach ($f in ($d | Get-ChildItem -Filter "*.flac" | % {$_.FullName}) ){
            $args=$args + """$f"" "
            }

        Start-Process "metaflac.exe" $args -Wait -NoNewWindow
    }

    # Process Subfolders
    foreach($dd in $d | Get-ChildItem -Directory){
        CallMetaFLAC $dd
        }
}

Write-Host "Starting in $root"
Write-Host

CallMetaFLAC $root

Write-Host
Write-Host "Ending"