@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "WORKDIR=%~dp0"
set "TEMPDIR=__mscz_tmp__"
set "MSCORE="
set "CAND[0]=%ProgramFiles%\MuseScore 4\bin\MuseScore4.exe"
set "CAND[1]=%ProgramFiles%\MuseScore 3\bin\MuseScore3.exe"
set "CAND[2]=%ProgramFiles(x86)%\MuseScore 4\bin\MuseScore4.exe"
set "CAND[3]=%ProgramFiles(x86)%\MuseScore 3\bin\MuseScore3.exe"
for /l %%i in (0,1,3) do if not defined MSCORE if exist "!CAND[%%i]!" set "MSCORE=!CAND[%%i]!"
if not defined MSCORE for %%e in (MuseScore4.exe MuseScore3.exe MuseScore.exe) do if not defined MSCORE for /f "delims=" %%p in ('where %%e 2^>nul') do if not defined MSCORE set "MSCORE=%%p"
if not defined MSCORE (
echo [ERROR] MuseScore not found. Please install it or add its bin folder to PATH.
pause
exit /b 1
)
echo Using MuseScore: "!MSCORE!"
echo Work folder : "%WORKDIR%"
echo.
powershell -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='Stop'; Add-Type -AssemblyName System.IO.Compression.FileSystem; $enc=New-Object System.Text.UTF8Encoding($false); $q=[char]34; $root=$env:WORKDIR; $msc=$env:MSCORE; $tmp=Join-Path $root $env:TEMPDIR; if(Test-Path -LiteralPath $tmp){Remove-Item -LiteralPath $tmp -Recurse -Force}; New-Item -ItemType Directory -Path $tmp | Out-Null; $files=Get-ChildItem -LiteralPath $root -Filter *.mscz -File; if($files.Count -eq 0){ Write-Host 'No .mscz files found.'; exit 0 }; foreach($f in $files){ Write-Host ('[file] ' + $f.Name); $base=[IO.Path]::GetFileNameWithoutExtension($f.Name); $pdf=Join-Path $root ($base+'.pdf'); $png=Join-Path $root ($base+'.png'); $xml=Join-Path $root ($base+'.musicxml'); $ex=Join-Path $tmp $base; if(Test-Path -LiteralPath $ex){Remove-Item -LiteralPath $ex -Recurse -Force}; [System.IO.Compression.ZipFile]::ExtractToDirectory($f.FullName,$ex); Get-ChildItem -LiteralPath $ex -Recurse -Filter *.mscx -File | ForEach-Object{ $t=[IO.File]::ReadAllText($_.FullName,$enc); $t=$t.Replace('0','1'); [IO.File]::WriteAllText($_.FullName,$t,$enc) }; $uh=Join-Path $tmp ($base+'.unhidden.mscz'); if(Test-Path -LiteralPath $uh){Remove-Item -LiteralPath $uh -Force}; [System.IO.Compression.ZipFile]::CreateFromDirectory($ex,$uh); $jobs=@(@($f.FullName,$pdf),@($f.FullName,$png),@($uh,$xml)); foreach($j in $jobs){ $p=Start-Process -FilePath $msc -ArgumentList @('-o',($q+$j[1]+$q),($q+$j[0]+$q)) -Wait -PassThru -NoNewWindow; Write-Host (' exit ' + $p.ExitCode + ' ' + (Split-Path $j[1] -Leaf)) } }; Remove-Item -LiteralPath $tmp -Recurse -Force -ErrorAction SilentlyContinue"
echo.
echo Done.
pause
endlocal