@ECHO off
SETLOCAL enableDelayedExpansion EnableExtensions
SET first=%1
IF NOT DEFINED first (CALL :_help & GOTO :eof)
FOR %%a IN (-h /h -help /help) DO (IF /I %%a==%first% ( CALL :_help & GOTO :eof ))
GOTO :_start
:_start
  SET lilyPath=\usr\bin\lilypond.exe
  (SET programPath=%~1)
  SET programName=%2
  SET version=%3
  SET inFile=%4
  SET inFileName=%~n4
	SET inPath=%~dp4
	IF NOT EXIST %inFile% (
	  ECHO %inFile% not found!
		GOTO :EOF
	) ELSE (
	  IF NOT "%inPath%"=="" PUSHD %inPath%
	 )
	)
  CALL :_inputControl
  FOR /f "TOKENS=*" %%a IN ('dir /B "%programPath%\%programName%%version%*"') DO (
    ECHO running "%programPath%\%%a%lilyPath%"
    SET _=%%a
    SET vers=!_:LilyPond=!& REM get the version
    SET stdout="!vers!-%inFileName%.txt"
    SET stderr="!vers!-%inFileName%.log"
    "%programPath%\%%a%lilyPath%" -o"!vers!-%inFileName%" %inFile% 1>!stdout! 2>!stderr!
    FINDSTR "error warning" !stderr!
  )
	IF NOT "%inPath%"=="" POPD
	ECHO(&ECHO done & PAUSE
	GOTO :eof
:_inputControl
  (SET line1=  inFile: %inFile:~1,-1% )
  CALL :_length "%line1%" str1
  ECHO(&ECHO your parameters:
  ECHO(&ECHO   search in: %programPath%
  ECHO(&ECHO   search item: %programName%
  ECHO(&ECHO   version: %version% 
  ECHO(&ECHO   inFile: %inFile%
  ECHO   %str1%
  ECHO( 
  REM ECHO inFileName %inFileName%
  GOTO :eof
:_length
  SETLOCAL
  (SET _length=)
  SET _substring=%~1
  (SET _string=)
  :_loop
	REM just to underline the file name
  IF NOT DEFINED _substring (ENDLOCAL & SET %2=%_string% & GOTO :eof)
  SET _substring=%_substring:~1%
  SET /A _length+=1
  IF NOT "%_substring%"=="" SET _string=%_string%-
  IF %_length%==99 (ECHO stopped loop & ENDLOCAL & GOTO :eof)
  GOTO :_loop  
  GOTO :eof
:_help
  ECHO -------------------------------------------------------------------------
  ECHO  parameters:
  ECHO  ----------- 
  ECHO  1. location of lilypond programs, e.g. 
  ECHO      "c:\program files\lilypond"
  ECHO      "c:\program files (x86^)\lilypond"
  ECHO      "H:\Lilypond\lilypond"
  ECHO(
  ECHO  2. name of the program, e.g. LilyPond 
  ECHO(
  ECHO  2. release, e.g. 2.13 - can be omitted with ""
  ECHO(
  ECHO  4. ly-file
  ECHO(
  ECHO  example: "...\lyMV.bat" "c:\program files (x86^)" "lilypond" 2.15 test.ly
  ECHO(
  ECHO      or - if lilypond is in the path:
  ECHO(
  ECHO    lymv "c:\program files (x86^)" lilypond 2.17 test.ly
  ECHO(
	ECHO -------------------------------------------------------------------------
  GOTO :eof